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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.ScmMsgConstants;
import com.hzs.common.core.enums.*;
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.scm.general.ext.IcGeneralOutBExt;
import com.hzs.common.domain.scm.general.ext.IcGeneralOutHExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.scm.general.controller.IcGeneralController;
import com.hzs.scm.general.param.GeneralParam;
import com.hzs.scm.general.service.IIcGeneralOutBService;
import com.hzs.scm.general.service.IIcGeneralOutHService;
import com.hzs.scm.general.vo.IcGeneralOutBVO;
import com.hzs.scm.general.vo.IcGeneralOutExcelVO;
import com.hzs.scm.general.vo.IcGeneralOutVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 其他出库单表头 前端控制器
 */
@RestController
@RequestMapping("/manage/general-out")
@Slf4j
public class IcGeneralOutHController extends IcGeneralController {

    @Autowired
    private IIcGeneralOutHService generalOutService;

    @Autowired
    private IIcGeneralOutBService generalOutBiService;

    /**
     * 查询列表
     *
     * @return cubasdocParam 入参
     */
    @PostMapping("/list")
    @Log(module = EOperationModule.GENERAL_OUT, business = EOperationBusiness.GENERAL_OUT, method = EOperationMethod.SELECT)
    public TableDataInfo listGeneralOut(@RequestBody GeneralParam generalParam) {
        startPage();
        generalParam.setPkCountry(SecurityUtils.getPkCountry());
        List<IcGeneralOutHExt> generalOutExtList = generalOutService.queryDisGeneralOutExtCondition(generalParam);
        return packageTableDataInfo(generalOutExtList, packageScmReturnVo(generalOutExtList));
    }

    /**
     * 查询待入库订单的明细
     *
     * @return cubasdocParam 入参
     */
    @GetMapping("/list-detail/{pkId}")
    public TableDataInfo queryGeneralOutDetail(@PathVariable Long pkId) {
        List<IcGeneralOutBExt> generalOutBiExtList = generalOutBiService.queryGeneralOutBiByPK(SecurityUtils.getPkCountry(), pkId);
        List<IcGeneralOutBVO> scmReturnVoList = new ArrayList<>();
        for (IcGeneralOutBExt generalOutBiExt : generalOutBiExtList) {
            scmReturnVoList.add(BeanUtil.copyProperties(generalOutBiExt, IcGeneralOutBVO.class));
        }
        return packageTableDataInfo(generalOutBiExtList, scmReturnVoList);
    }

    private List<IcGeneralOutVO> packageScmReturnVo(List<IcGeneralOutHExt> generalOutExtList) {
        List<IcGeneralOutVO> generalOutList = new ArrayList<>();
        for (IcGeneralOutHExt generalOutExt : generalOutExtList) {
            IcGeneralOutVO generalOutVO = BeanUtil.copyProperties(generalOutExt, IcGeneralOutVO.class);
            List<IcGeneralOutBExt> generalOutbExtList = generalOutExt.getGeneralOutbExtList();
            if (CollectionUtil.isNotEmpty(generalOutbExtList)) {
                List<IcGeneralOutBVO> returnbVoList = BeanUtil.copyToList(generalOutbExtList, IcGeneralOutBVO.class);
                generalOutVO.setGeneralOutbExtList(returnbVoList);
            }
            generalOutList.add(generalOutVO);
        }
        return generalOutList;
    }

    /**
     * 分页查询会员某个时间段内的奖金汇总
     */
    @PostMapping("/export-list-total")
    @Log(module = EOperationModule.GENERAL_IN, business = EOperationBusiness.GENERAL_IN, method = EOperationMethod.EXPORT)
    public void exportListTransInTotal(HttpServletResponse response, GeneralParam generalParam) {
        generalParam.setPkCountry(SecurityUtils.getPkCountry());
        List<IcGeneralOutHExt> generalOutExtList = generalOutService.queryDisGeneralOutExtCondition(generalParam);
        List<IcGeneralOutExcelVO> generalOutExcelVoList = packageScmExportVo(generalOutExtList);
        ExcelUtil<IcGeneralOutExcelVO> util = new ExcelUtil<>(IcGeneralOutExcelVO.class);
        util.exportExcel(response, generalOutExcelVoList, "其他出库导出");
    }

    private List<IcGeneralOutExcelVO> packageScmExportVo(List<IcGeneralOutHExt> generalOutExtList) {
        List<IcGeneralOutExcelVO> generalOutExcelVoList = new ArrayList<>();
        for (IcGeneralOutHExt generalOutExt : generalOutExtList) {
            List<IcGeneralOutBExt> generalOutbExtList = generalOutExt.getGeneralOutbExtList();
            for (IcGeneralOutBExt generalOutBiExt : generalOutbExtList) {
                IcGeneralOutExcelVO generalOutExcelVo = BeanUtil.copyProperties(generalOutExt, IcGeneralOutExcelVO.class);
                setScmItemsGeneralExcel(generalOutBiExt, generalOutExcelVo);
                generalOutExcelVo.setBizDate(generalOutBiExt.getBizDate());
                generalOutExcelVo.setNcostprice(generalOutBiExt.getNcostprice());
                generalOutExcelVo.setNcostmny(generalOutBiExt.getNcostmny());
                generalOutExcelVoList.add(generalOutExcelVo);
            }
        }
        return generalOutExcelVoList;
    }

    /**
     * 新增其他入库
     *
     * @param generalParam 入参
     */
    @PostMapping("/insert")
    @Log(module = EOperationModule.GENERAL_OUT, business = EOperationBusiness.GENERAL_OUT, method = EOperationMethod.INSERT)
    public AjaxResult saveGeneralOut(@RequestBody GeneralParam generalParam) {
        String message = validateGeneralParam(generalParam);
        if (ObjectUtil.isNotEmpty(message)) {
            return AjaxResult.error(TransactionUtils.getContent(message));
        }
        generalOutService.insertGeneralOutByGeneralParam(generalParam, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

    /**
     * 修改其他入库
     *
     * @param generalParam 入参
     */
    @PostMapping("/update")
    @Log(module = EOperationModule.GENERAL_OUT, business = EOperationBusiness.GENERAL_OUT, method = EOperationMethod.UPDATE)
    public AjaxResult updatePoOrder(@RequestBody GeneralParam generalParam) {
        if (generalParam.getPkId() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.IC_GENERAL_OUT_NOT_EXIST));
        }
        String message = validateGeneralParam(generalParam);
        if (ObjectUtil.isNotEmpty(message)) {
            return AjaxResult.error(TransactionUtils.getContent(message));
        }
        if (generalParam.getPkId() == null) {
            generalOutService.insertGeneralOutByGeneralParam(generalParam, SecurityUtils.getUserId());
        } else {
            // 查询是否存在其他入库单
            IcGeneralOutHExt generalOutExt = generalOutService.queryGeneralOutById(generalParam.getPkId(), EScmOrderStatus.SAVE.getValue());
            if (generalOutExt == null) {
                return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.IC_GENERAL_OUT_NOT_EXIST));
            }
            generalOutService.updateGeneralOutByGeneralParam(generalOutExt, generalParam, SecurityUtils.getUserId());
        }
        return AjaxResult.success();
    }

    /**
     * 删除其他入库
     *
     * @param pkId 主键
     */
    @DeleteMapping("/{pkId}")
    @Log(module = EOperationModule.GENERAL_OUT, business = EOperationBusiness.GENERAL_OUT, method = EOperationMethod.DELETE)
    public AjaxResult delete(@PathVariable Long pkId) {
        if (pkId == null) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.IC_GENERAL_OUT_NOT_EXIST));
        }
        IcGeneralOutHExt generalOutExt = generalOutService.queryGeneralOutById(pkId, EScmOrderStatus.SAVE.getValue());
        if (generalOutExt == null) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.IC_GENERAL_OUT_NOT_EXIST));
        }
        generalOutService.deleteGeneralOut(pkId, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

}
