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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.storegoodsinventory.vo.AddReqVO;
import cn.iocoder.yudao.module.member.controller.admin.storeorder.vo.StoreOrderPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.storeorder.vo.StoreOrderRespVO;
import cn.iocoder.yudao.module.member.controller.app.storeorder.vo.AppStoreOrderDetailVo;
import cn.iocoder.yudao.module.member.controller.app.storeorder.vo.AppStoreOrderVO;
import cn.iocoder.yudao.module.member.controller.app.storeorder.vo.CardDto;
import cn.iocoder.yudao.module.member.dal.dataobject.storeorder.StoreOrderDO;
import cn.iocoder.yudao.module.member.enums.StoreOrderStatusEnum;
import cn.iocoder.yudao.module.member.service.async.MemberAsyncService;
import cn.iocoder.yudao.module.member.service.storeorder.StoreOrderService;
import cn.iocoder.yudao.module.member.service.storeorderdetail.StoreOrderDetailService;
import cn.iocoder.yudao.module.system.api.carteen.CarteenApi;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "APP超市 - 商品订单")
@RestController
@RequestMapping("/member/store-order")
@Validated
public class AppStoreOrderController {

    @Resource
    private StoreOrderService storeOrderService;
    @Resource
    private StoreOrderDetailService orderDetailService;
    @Resource
    private CarteenApi carteenApi;
    @Resource
    private MemberAsyncService memberAsyncService;


    @GetMapping("/page")
    @Operation(summary = "获得商品订单分页")
    public CommonResult<PageResult<StoreOrderRespVO>> getStoreOrderPage(@Valid StoreOrderPageReqVO pageReqVO) {
        if (pageReqVO.getUserId() == null) {
            pageReqVO.setStatus(StoreOrderStatusEnum.COMPLETE.getCode());
        }
        PageResult<StoreOrderDO> pageResult = storeOrderService.getStoreOrderPage(pageReqVO);
        PageResult<StoreOrderRespVO> bean = BeanUtils.toBean(pageResult, StoreOrderRespVO.class);
        //处理订单详情
        for (StoreOrderRespVO vo : bean.getList()) {
            List<AppStoreOrderDetailVo> listByOrderNo = orderDetailService.getListByOrderNo(vo.getOrderId());
            vo.setDetailDOS(listByOrderNo);
            vo.setStoresName(carteenApi.getCarteenById(vo.getCarteenId()).getStoresName());
        }
        return success(bean);
    }

    @PostMapping("/appCreate")
    @Operation(summary = "购物车订单")
    public CommonResult<Boolean> appCreate(@RequestBody CardDto dto) {


        AddReqVO addReqVO = storeOrderService.appCreate(dto);
        //记录
        memberAsyncService.batchRecord(addReqVO);

        return success(true);
    }

    @GetMapping("/code")
    @Operation(summary = "二维码")
    public CommonResult<String> code(Integer orderId) {
        return success(storeOrderService.code(orderId));
    }

    @GetMapping("/info")
    @Operation(summary = "二维码商品信息")
    public CommonResult<AppStoreOrderVO> info(String code) {
        return success(storeOrderService.info(code));
    }

    @GetMapping("/complete")
    @Operation(summary = "核销")
    public CommonResult<Boolean> complete(Integer orderId) {
        return success(storeOrderService.complete(orderId));
    }

    @GetMapping("/cancel")
    @Operation(summary = "取消订单")
    public synchronized CommonResult<Boolean> cancel(Integer orderId) {

        AddReqVO cancel = storeOrderService.cancel(orderId);
        //记录
        memberAsyncService.batchRecord(cancel);

        return success(true);
    }

    @GetMapping("/refund")
    @Operation(summary = "退款")
    public CommonResult<Long> refund(Integer orderId) {
        return success(storeOrderService.refund(orderId));
    }

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