package com.hzs.report.scm.controller;

import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.HttpStatus;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.scm.trans.ext.IcTransInHExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.report.scm.InventorySaleParam;
import com.hzs.report.scm.service.IInventoryService;
import com.hzs.report.scm.vo.InventorySaleVO;
import lombok.extern.slf4j.Slf4j;
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.time.temporal.ChronoUnit;
import java.util.List;

/**
 * @description: 进销存报表统计
 * @author: sui q
 * @time: 2024/8/2 10:56
 * @classname: ScmReportController
 * @package_name: com.hzs.report.scm.controller
 * version 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/manage/inventory")
public class InventoryController extends BaseController {

    @Autowired
    private IInventoryService inventoryService;

    /**
     * 查询列表
     *
     * @return cubasdocParam 入参
     */
    @PostMapping("/list")
    public TableDataInfo listInventorySale(@RequestBody InventorySaleParam inventorySaleParam) {
        if(inventorySaleParam.getStartDate() == null || inventorySaleParam.getEndDate() == null){
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.ERROR);
            rspData.setMsg("开始时间，结束时间不能为空");
            return rspData;
        }
//        inventorySaleParam.setPkCountry(SecurityUtils.getPkCountry());
        startPage();
        return getDataTable(inventoryService.queryInventorySaleListByParam(inventorySaleParam));
    }

    /**
     * 分页查询会员某个时间段内的库存销量
     */
    @PostMapping("/export")
    public void exportInventorySale(HttpServletResponse response, @RequestBody InventorySaleParam inventorySaleParam){
        if(inventorySaleParam.getStartDate() == null || inventorySaleParam.getEndDate() == null){
            return;
        }
        inventorySaleParam.setPkCountry(SecurityUtils.getPkCountry());
        List<InventorySaleVO> inventorySaleVOList = inventoryService.queryInventorySaleListByParam(inventorySaleParam);
        for (InventorySaleVO inventorySaleVO : inventorySaleVOList) {
            if(EDealerYesNo.YES.getValue() == inventorySaleVO.getIsDue()){
                inventorySaleVO.setIsDueVal(EDealerYesNo.YES.getLabel());
            }else{
                inventorySaleVO.setIsDueVal(EDealerYesNo.NO.getLabel());
            }
        }
        ExcelUtil<InventorySaleVO> util = new ExcelUtil<>(InventorySaleVO.class);
        util.exportExcel(response, inventorySaleVOList, "库存销量导出");
    }
}
