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

import cn.hutool.core.bean.BeanUtil;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.enums.EShippingChannel;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.scm.ic.ext.IcOnHandNumExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.scm.ic.param.OnHandNumParam;
import com.hzs.scm.ic.service.IIcOnHandNumService;
import com.hzs.scm.ic.vo.IcOnHandNumVo;
import com.hzs.scm.pub.controller.ScmController;
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;

/**
 * @description: 现存量控制器
 * @author: sui q
 * @time: 2023/12/20 14:09
 * @classname: IcOnHandNumController
 * @package_name: com.hzs.scm.ic.controller.manage
 * version 1.0.0
 */
@RestController
@RequestMapping("/manage/hand-num")
public class IcOnHandNumController extends ScmController {

    @Autowired
    private IIcOnHandNumService onHandNumService;

    /**
     * 查询列表
     *
     * @return cubasdocParam 入参
     */
    @PostMapping("/list")
    @Log(module = EOperationModule.ON_HAND_NUM, business = EOperationBusiness.ON_HAND_NUM, method = EOperationMethod.SELECT)
    public TableDataInfo listOnHandNum(@RequestBody OnHandNumParam onHandNumParam) {
        startPage();
        onHandNumParam.setPkCountry(SecurityUtils.getPkCountry());
        List<IcOnHandNumExt> onHandNumExtList = onHandNumService.queryInventoryProductByParam(onHandNumParam);
        return getDataTable(onHandNumExtList);
    }


    /**
     * 分页查询会员某个时间段内的奖金汇总
     */
    @PostMapping("/export-list")
    @Log(module = EOperationModule.ON_HAND_NUM, business = EOperationBusiness.ON_HAND_NUM, method = EOperationMethod.EXPORT)
    public void exportListOnHandNum(HttpServletResponse response, OnHandNumParam onHandNumParam){
        onHandNumParam.setPkCountry(SecurityUtils.getPkCountry());
        List<IcOnHandNumExt> onHandNumExtList = onHandNumService.queryInventoryProductByParam(onHandNumParam);
        List<IcOnHandNumVo> onhandNumList = packageScmExportVo(onHandNumExtList);
        ExcelUtil<IcOnHandNumVo> util = new ExcelUtil<>(IcOnHandNumVo.class);
        util.exportExcel(response, onhandNumList, "现存量导出");
    }

    private List<IcOnHandNumVo> packageScmExportVo(List<IcOnHandNumExt> onHandNumExtList) {
        List<IcOnHandNumVo> onhandNumList = new ArrayList<>();
        for (IcOnHandNumExt onHandNumExt : onHandNumExtList) {
            IcOnHandNumVo onHandNumVo = BeanUtil.copyProperties(onHandNumExt, IcOnHandNumVo.class);
            for (EShippingChannel value : EShippingChannel.values()) {
                if(value.getValue().equals(onHandNumExt.getShippingChannel())){
                    onHandNumVo.setShippingChannelVal(value.getLabel());
                    break;
                }
            }
            onhandNumList.add(onHandNumVo);
        }
        return onhandNumList;
    }
}
