package com.gree.mall.manager.controller.engin;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.engin.OrderEnginBaseVO;
import com.gree.mall.manager.bean.engin.WebsitSalesManagerDetail;
import com.gree.mall.manager.bean.engin.WebsitSalesManagerVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.engin.WebsitGoodsMangerLogic;
import com.gree.mall.manager.plus.entity.WebsitSalesManagerPrice;
import com.gree.mall.manager.plus.mapper.WebsitGoodsMapper;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

@Slf4j
@RestController
@Api(value = "工程辅材/配件收款管理", tags = {"工程辅材/配件收款管理"})
@Validated
@RequestMapping(value = "/order/engin/manger", produces = "application/json; charset=utf-8")
public class OrderEnginManagerController {

    @Resource
    WebsitGoodsMangerLogic websitGoodsMangerLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "工程辅材/配件收款-列表")
    public ResponseHelper<Page<WebsitSalesManagerVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitSalesManagerVO> vo = websitGoodsMangerLogic.list(zfireParamBean);
        return ResponseHelper.success(vo, new TypeReference<WebsitSalesManagerVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "工程辅材/配件收款-导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<WebsitSalesManagerVO> page = websitGoodsMangerLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "工程辅材/配件收款-详情")
    public ResponseHelper<WebsitSalesManagerDetail> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws RemoteServiceException {
        WebsitSalesManagerDetail bean = websitGoodsMangerLogic.detail(id);
        return ResponseHelper.success(bean);
    }


    @PostMapping("/detailPrice")
    @ApiOperation(value = "工程辅材/配件收款-收款信息")
    public ResponseHelper<List<WebsitSalesManagerPrice>> detailPrice(
            @ApiParam(value = "工程机编号", required = true) @RequestParam String orderEnginBaseId,
            @ApiParam(value = "商品类型 M=辅材 P=配件", required = true) @RequestParam String goodsType
    ) throws RemoteServiceException {
        List<WebsitSalesManagerPrice> bean = websitGoodsMangerLogic.detailPrice(orderEnginBaseId,goodsType);
        return ResponseHelper.success(bean);
    }

    @PostMapping("/givePrice")
    @ApiOperation(value = "工程辅材/配件收款-提交收款信息")
    public ResponseHelper givePrice(
            @ApiParam(value = "工程机编号", required = true) @RequestParam(required = true) String orderEnginBaseId,
            @ApiParam(value = "金额", required = true) @RequestParam(required = true) BigDecimal price,
            @ApiParam(value = "凭证", required = true) @RequestParam(required = true) String urlPrc,
            @ApiParam(value = "备注", required = false) @RequestParam(required = false) String remark,
            @ApiParam(value = "关联订单", required = true) @RequestParam(required = true) String orderId,
            @ApiParam(value = "类型", required = true) @RequestParam(required = true) String goodsType
    ) throws RemoteServiceException {
        if (price.compareTo(new BigDecimal("0")) == -1)
            throw new RemoteServiceException("金额不能为负数");
        websitGoodsMangerLogic.givePrice(price,orderEnginBaseId,urlPrc,remark,orderId,goodsType);
        return ResponseHelper.success();
    }

}
