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.IcGeneralInBExt;
import com.hzs.common.domain.scm.general.ext.IcGeneralInHExt;
import com.hzs.common.domain.scm.pub.ScmOrderApproveLog;
import com.hzs.common.security.service.UserTokenService;
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.IIcGeneralInBService;
import com.hzs.scm.general.service.IIcGeneralInHService;
import com.hzs.scm.general.vo.IcGeneralInBVO;
import com.hzs.scm.general.vo.IcGeneralInExcelVO;
import com.hzs.scm.general.vo.IcGeneralInVO;
import com.hzs.scm.pub.service.IScmOrderApproveLogService;
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.ArrayList;
import java.util.List;

/**
 * 其他入库表头 前端控制器
 */
@RestController
@RequestMapping("/manage/general-in")
@Slf4j
public class IcGeneralInHController extends IcGeneralController {

    @Autowired
    private IIcGeneralInHService generalInhService;
    @Autowired
    private IIcGeneralInBService generalInBiService;

    /**
     * 查询列表
     *
     * @return cubasdocParam 入参
     */
    @PostMapping("/list")
    @Log(module = EOperationModule.GENERAL_IN, business = EOperationBusiness.GENERAL_IN, method = EOperationMethod.SELECT)
    public TableDataInfo listGeneralIn(@RequestBody GeneralParam generalParam) {
        startPage();
        generalParam.setPkCountry(SecurityUtils.getPkCountry());
        List<IcGeneralInHExt> generalInExtList = generalInhService.queryDisGeneralInExtByCondition(generalParam);
        return packageTableDataInfo(generalInExtList, packageScmReturnVo(generalInExtList));
    }

    private List<IcGeneralInVO> packageScmReturnVo(List<IcGeneralInHExt> generalInExtList) {
        List<IcGeneralInVO> generalInList = new ArrayList<>();
        for (IcGeneralInHExt generalInExt : generalInExtList) {
            IcGeneralInVO icGeneralInVO = BeanUtil.copyProperties(generalInExt, IcGeneralInVO.class);
            List<IcGeneralInBExt> generalInbExtList = generalInExt.getGeneralInbExtList();
            if (CollectionUtil.isNotEmpty(generalInbExtList)) {
                List<IcGeneralInBVO> returnbVoList = BeanUtil.copyToList(generalInbExtList, IcGeneralInBVO.class);
                icGeneralInVO.setGeneralInbExtList(returnbVoList);
            }
            generalInList.add(icGeneralInVO);
        }
        return generalInList;
    }

    /**
     * 查询待入库订单的明细
     *
     * @return cubasdocParam 入参
     */
    @GetMapping("/list-detail/{pkId}")
    public TableDataInfo queryGeneralInDetail(@PathVariable Long pkId) {
        List<IcGeneralInBExt> generalInBiExtList = generalInBiService.queryGeneralInbByPk(SecurityUtils.getPkCountry(), pkId);
        List<IcGeneralInBVO> scmReturnVoList = new ArrayList<>();
        for (IcGeneralInBExt generalInExt : generalInBiExtList) {
            scmReturnVoList.add(BeanUtil.copyProperties(generalInExt, IcGeneralInBVO.class));
        }
        return packageTableDataInfo(generalInBiExtList, scmReturnVoList);
    }

    /**
     * 分页查询会员某个时间段内的奖金汇总
     */
    @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<IcGeneralInHExt> generalInExtList = generalInhService.queryDisGeneralInExtByCondition(generalParam);
        List<IcGeneralInExcelVO> generalInExcelVoList = packageScmExportVo(generalInExtList);
        ExcelUtil<IcGeneralInExcelVO> util = new ExcelUtil<>(IcGeneralInExcelVO.class);
        util.exportExcel(response, generalInExcelVoList, "其他入库导出");
    }

    private List<IcGeneralInExcelVO> packageScmExportVo(List<IcGeneralInHExt> generalInExtList) {
        List<IcGeneralInExcelVO> generalInExcelVoList = new ArrayList<>();
        for (IcGeneralInHExt generalInExt : generalInExtList) {
            List<IcGeneralInBExt> generalInbExtList = generalInExt.getGeneralInbExtList();
            for (IcGeneralInBExt generalInBiExt : generalInbExtList) {
                IcGeneralInExcelVO generalInExcelVo = BeanUtil.copyProperties(generalInExt, IcGeneralInExcelVO.class);
                setScmItemsGeneralExcel(generalInBiExt, generalInExcelVo);
                generalInExcelVo.setBizDate(generalInBiExt.getBizDate());
                generalInExcelVo.setNcostprice(generalInBiExt.getNcostprice());
                generalInExcelVo.setNcostmny(generalInBiExt.getNcostmny());
                generalInExcelVoList.add(generalInExcelVo);
            }
        }
        return generalInExcelVoList;
    }

    /**
     * 新增其他入库
     *
     * @param generalParam 入参
     */
    @PostMapping("/insert")
    @Log(module = EOperationModule.GENERAL_IN, business = EOperationBusiness.GENERAL_IN, method = EOperationMethod.INSERT)
    public AjaxResult saveGeneralIn(@RequestBody GeneralParam generalParam) {
        String message = validateGeneralParam(generalParam);
        if (ObjectUtil.isNotEmpty(message)) {
            return AjaxResult.error(TransactionUtils.getContent(message));
        }
        generalInhService.insertGeneralInByGeneralParam(generalParam, SecurityUtils.getUserId());
        // 审核通过后需要关闭采购订单 回写采购订单累计入库数量
        return AjaxResult.success();
    }

    /**
     * 修改其他入库
     *
     * @param generalParam 入参
     */
    @PostMapping("/update")
    @Log(module = EOperationModule.GENERAL_IN, business = EOperationBusiness.GENERAL_IN, method = EOperationMethod.UPDATE)
    public AjaxResult updatePoOrder(@RequestBody GeneralParam generalParam) {
        if (generalParam.getPkId() == null) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.IC_GENERAL_IN_NOT_EXIST));
        }
        String message = validateGeneralParam(generalParam);
        if (ObjectUtil.isNotEmpty(message)) {
            return AjaxResult.error(TransactionUtils.getContent(message));
        }
        // 查询是否存在其他入库单
        IcGeneralInHExt generalInhExt = generalInhService.queryGeneralInById(generalParam.getPkId(), EScmOrderStatus.SAVE.getValue());
        if (generalInhExt == null) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.IC_GENERAL_IN_NOT_EXIST));
        }
        generalInhService.updateGeneralInByGeneralParam(generalInhExt, generalParam, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

    /**
     * 删除其他入库
     *
     * @param pkId 主键
     */
    @DeleteMapping("/{pkId}")
    @Log(module = EOperationModule.GENERAL_IN, business = EOperationBusiness.GENERAL_IN, method = EOperationMethod.DELETE)
    public AjaxResult delete(@PathVariable Long pkId) {
        if (pkId == null) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.IC_GENERAL_IN_NOT_EXIST));
        }
        IcGeneralInHExt generalInhExt = generalInhService.queryGeneralInById(pkId, EScmOrderStatus.SAVE.getValue());
        if (generalInhExt == null) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.IC_GENERAL_IN_NOT_EXIST));
        }
        generalInhService.deleteGeneralIn(pkId, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

}
