package cn.iocoder.yudao.module.member.controller.admin.storeorder;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.storeorder.vo.StoreOrderCustomizeVO;
import cn.iocoder.yudao.module.member.controller.admin.storeorder.vo.StoreOrderDetailVO;
import cn.iocoder.yudao.module.member.controller.admin.storeorder.vo.StoreOrderPageVO;
import cn.iocoder.yudao.module.member.controller.admin.storeorder.vo.StoreOrderSaveReqVO;
import cn.iocoder.yudao.module.member.controller.app.store.dto.StoreOrderDto;
import cn.iocoder.yudao.module.member.controller.app.storeorder.vo.AppStoreOrderDetailVo;
import cn.iocoder.yudao.module.member.dal.dataobject.storeorder.StoreOrderDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.storeorder.StoreOrderService;
import cn.iocoder.yudao.module.member.service.storeorderdetail.StoreOrderDetailService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import cn.iocoder.yudao.module.system.api.cashregisterinfo.CashregisterinfoApi;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 商品订单")
@RestController
@RequestMapping("/member/store-order")
@Validated
public class StoreOrderController {

    @Resource
    private StoreOrderService storeOrderService;
    @Resource
    private MemberUserService userService;
    @Resource
    private CashregisterinfoApi cashregisterinfoApi;
    @Resource
    private StoreOrderDetailService orderDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建商品订单")
    @PreAuthorize("@ss.hasPermission('member:store-order:create')")
    public CommonResult<Integer> createStoreOrder(@Valid @RequestBody StoreOrderSaveReqVO createReqVO) {
        return success(storeOrderService.createStoreOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品订单")
    @PreAuthorize("@ss.hasPermission('member:store-order:update')")
    public CommonResult<Boolean> updateStoreOrder(@Valid @RequestBody StoreOrderSaveReqVO updateReqVO) {
        storeOrderService.updateStoreOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:store-order:delete')")
    public CommonResult<Boolean> deleteStoreOrder(@RequestParam("id") Integer id) {
        storeOrderService.deleteStoreOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:store-order:query')")
    public CommonResult<StoreOrderDetailVO> getStoreOrder(@RequestParam("id") Integer id) {
        StoreOrderDO storeOrder = storeOrderService.getStoreOrder(id);
        StoreOrderDetailVO storeOrderDetailVO = new StoreOrderDetailVO();
        BeanUtil.copyProperties(storeOrder, storeOrderDetailVO);
        MemberUserDO user = userService.getUser(storeOrder.getUserId());
        storeOrderDetailVO.setNickName(user.getNickname());
        storeOrderDetailVO.setMobile(user.getMobile());
//        String storeName = cashregisterinfoApi.getStoreName(storeOrder.getEquipmentCode());
//        storeOrderDetailVO.setStoresName(storeName);
        List<AppStoreOrderDetailVo> listByOrderNo = orderDetailService.getListByOrderNo(storeOrder.getOrderId());
        storeOrderDetailVO.setDetailDOS(listByOrderNo);
        return success(storeOrderDetailVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品订单分页")
    @PreAuthorize("@ss.hasPermission('member:store-order:query')")
    public CommonResult<PageResult<StoreOrderPageVO>> getStoreOrderPage(@Valid StoreOrderPageVO pageReqVO) {
        PageResult<StoreOrderPageVO> storeOrderPageNew = storeOrderService.getStoreOrderPageNew(pageReqVO);
        return success(storeOrderPageNew);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品订单 Excel")
    @PreAuthorize("@ss.hasPermission('member:store-order:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreOrderExcel(@Valid StoreOrderPageVO pageReqVO,
              HttpServletResponse response) throws IOException {
        List<StoreOrderPageVO> list = storeOrderService.getStoreOrderPageNewList(pageReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "商品订单统计.xls", "数据", StoreOrderPageVO.class,
                list );
    }

    @GetMapping("/reduction")
    @Operation(summary = "减免")
    public CommonResult<Boolean> reduction(Long orderId, BigDecimal money){
        storeOrderService.reduction(orderId,money);
        return CommonResult.success(true);
    }

    @PostMapping("/customize")
    @Operation(summary = "自定义扣款")
    public CommonResult<Boolean> customize(@RequestBody StoreOrderCustomizeVO dto){
        storeOrderService.customize(dto);
        return CommonResult.success(true);
    }

}