package com.hzs.scm.sale.controller.manage;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.core.constant.msg.ScmMsgConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.sale.ext.SaDeliverExt;
import com.hzs.common.domain.scm.pub.ScmOrderApproveLog;
import com.hzs.common.domain.scm.sale.ext.IcSaleoutBExt;
import com.hzs.common.domain.scm.sale.ext.IcSaleoutHExt;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.scm.pub.controller.ScmController;
import com.hzs.scm.pub.param.ScmApproveParam;
import com.hzs.scm.pub.service.IScmOrderApproveLogService;
import com.hzs.scm.sale.param.SaleOutParam;
import com.hzs.scm.sale.service.IIcSaleoutHService;
import com.hzs.scm.sale.vo.SaleOutBVO;
import com.hzs.scm.sale.vo.SaleOutBackExcelVO;
import com.hzs.scm.sale.vo.SaleOutExcelVO;
import com.hzs.scm.sale.vo.SaleOutHVO;
import org.springframework.beans.factory.annotation.Autowired;
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.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 销售出库单表头 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2023-11-29
 */
@RestController
@RequestMapping("/manage/sale-out")
public class IcSaleoutHController extends ScmController {

    @Autowired
    private IIcSaleoutHService saleOutService;

    @Autowired
    private UserTokenService userTokenService;

    @Autowired
    private IScmOrderApproveLogService scmOrderApproveLogService;

    /**
     * 查询列表
     *
     * @return cubasdocParam 入参
     */
    @PostMapping("/list")
    @Log(module = EOperationModule.SALE_OUT, business = EOperationBusiness.SALE_OUT, method = EOperationMethod.SELECT)
    public TableDataInfo listSaleOut(@RequestBody SaleOutParam saleOutParam) {
        startPage();
        saleOutParam.setPkCountry(SecurityUtils.getPkCountry());
        saleOutParam.setReturnStatus(EDealerYesNo.NO.getValue());
        List<IcSaleoutHExt> saleOutHiList = saleOutService.queryDisSaleOutExtCondition(saleOutParam);
        return packageTableDataInfo(saleOutHiList, packageScmReturnVo(saleOutHiList));
    }

    /**
     * 查询列表
     *
     * @return cubasdocParam 入参
     */
    @PostMapping("/list-back")
    @Log(module = EOperationModule.SALE_OUT, business = EOperationBusiness.SALE_OUT, method = EOperationMethod.SELECT)
    public TableDataInfo listBackSaleOut(@RequestBody SaleOutParam saleOutParam) {
        startPage();
        saleOutParam.setPkCountry(SecurityUtils.getPkCountry());
        saleOutParam.setReturnStatus(EDealerYesNo.YES.getValue());
        List<IcSaleoutHExt> saleOutHiList = saleOutService.queryDisSaleOutExtCondition(saleOutParam);
        return packageTableDataInfo(saleOutHiList, packageScmReturnVo(saleOutHiList));
    }

    private List<SaleOutHVO> packageScmReturnVo(List<IcSaleoutHExt> saleOutHiList) {
        List<SaleOutHVO> saleOutHiVOList = new ArrayList<>();
        for (IcSaleoutHExt saleOutHiExt : saleOutHiList) {
            SaleOutHVO saleOutHiVo = BeanUtil.copyProperties(saleOutHiExt, SaleOutHVO.class);
            List<IcSaleoutBExt> saleoutbExtList = saleOutHiExt.getSaleoutbExtList();
            if (CollectionUtil.isNotEmpty(saleoutbExtList)) {
                List<SaleOutBVO> returnbVoList = BeanUtil.copyToList(saleoutbExtList, SaleOutBVO.class);
                saleOutHiVo.setSaleOutBiVoList(returnbVoList);
            }
            saleOutHiVOList.add(saleOutHiVo);
        }
        return saleOutHiVOList;
    }

    /**
     * 分页查询会员某个时间段内的奖金汇总
     */
    @PostMapping("/export-list-total")
    @Log(module = EOperationModule.SALE_OUT, business = EOperationBusiness.SALE_OUT, method = EOperationMethod.EXPORT)
    public void exportListSaleOutTotal(HttpServletResponse response, SaleOutParam saleOutParam) {
        saleOutParam.setPkCountry(SecurityUtils.getPkCountry());
        saleOutParam.setReturnStatus(EDealerYesNo.NO.getValue());
        List<IcSaleoutHExt> saleOutHiList = saleOutService.queryDisSaleOutExtCondition(saleOutParam);
        List<SaleOutExcelVO> saleOutExcelVoList = packageScmExportVo(saleOutHiList);
        ExcelUtil<SaleOutExcelVO> util = new ExcelUtil<>(SaleOutExcelVO.class);
        util.exportExcel(response, saleOutExcelVoList, "销售出库导出");
    }

    private List<SaleOutExcelVO> packageScmExportVo(List<IcSaleoutHExt> saleOutHiList) {
        List<SaleOutExcelVO> saleOutExcelVoList = new ArrayList<>();
        for (IcSaleoutHExt saleOutExt : saleOutHiList) {
            List<IcSaleoutBExt> saleOutBiExtList = saleOutExt.getSaleoutbExtList();
            for (IcSaleoutBExt saleOutBiExt : saleOutBiExtList) {
                SaleOutExcelVO saleOutExcelVo = BeanUtil.copyProperties(saleOutExt, SaleOutExcelVO.class);
                setScmItemsExcel(saleOutBiExt, saleOutExcelVo);
                saleOutExcelVoList.add(saleOutExcelVo);
            }
        }
        return saleOutExcelVoList;
    }

    /**
     * 分页查询会员某个时间段内的奖金汇总
     */
    @PostMapping("/export-back-total")
    @Log(module = EOperationModule.SALE_OUT, business = EOperationBusiness.SALE_OUT, method = EOperationMethod.EXPORT)
    public void exportListBackSaleOutTotal(HttpServletResponse response, SaleOutParam saleOutParam) {
        saleOutParam.setPkCountry(SecurityUtils.getPkCountry());
        saleOutParam.setReturnStatus(EDealerYesNo.YES.getValue());
        List<IcSaleoutHExt> saleOutHiList = saleOutService.queryDisSaleOutExtCondition(saleOutParam);
        List<SaleOutBackExcelVO> saleOutExcelVoList = packageBackScmExportVo(saleOutHiList);
        ExcelUtil<SaleOutBackExcelVO> util = new ExcelUtil<>(SaleOutBackExcelVO.class);
        util.exportExcel(response, saleOutExcelVoList, "销售退货导出");
    }

    private List<SaleOutBackExcelVO> packageBackScmExportVo(List<IcSaleoutHExt> saleOutHiList) {
        List<SaleOutBackExcelVO> saleOutExcelVoList = new ArrayList<>();
        for (IcSaleoutHExt saleOutExt : saleOutHiList) {
            List<IcSaleoutBExt> saleOutBiExtList = saleOutExt.getSaleoutbExtList();
            for (IcSaleoutBExt saleOutBiExt : saleOutBiExtList) {
                SaleOutBackExcelVO saleOutExcelVo = BeanUtil.copyProperties(saleOutExt, SaleOutBackExcelVO.class);
                setScmItemsExcel(saleOutBiExt, saleOutExcelVo);
                saleOutExcelVoList.add(saleOutExcelVo);
            }
        }
        return saleOutExcelVoList;
    }

    /**
     * 新增销售出库单，用于测试
     *
     * @param deliverExt 入参
     */
    @PostMapping("/insert")
    public AjaxResult saveSaleOut(@RequestBody SaDeliverExt deliverExt) {
        saleOutService.saveSaleOutBySaDeliver(deliverExt);
        return AjaxResult.success();
    }

    /**
     * 退货
     *
     * @param saleOutParam 入参
     */
    @PostMapping("/back")
    @Log(module = EOperationModule.SALE_OUT, business = EOperationBusiness.SALE_OUT, method = EOperationMethod.BACK)
    public AjaxResult backSaleOut(@RequestBody SaleOutParam saleOutParam) {
        if (saleOutParam.getPkId() == null) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY));
        }
        // 验证是否存在销售出库单
        saleOutParam.setPkCountry(SecurityUtils.getPkCountry());
        saleOutParam.setOrderStatus(EScmOrderStatus.CLOSE.getValue());
        IcSaleoutHExt saleOutExt = saleOutService.querySaleOutExtById(saleOutParam);
        if (saleOutExt == null) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.SALE_OUT_BACK_NOT_EXIST));
        }
        saleOutService.saveBackSaleOutByParam(saleOutExt, saleOutParam);
        return AjaxResult.success();
    }

}
