package com.sckw.contract.controller;

import com.sckw.contract.api.model.dto.req.SalesReportGoodsQueryDto;
import com.sckw.contract.api.model.dto.req.TradeEntListQueryFeignDto;
import com.sckw.contract.api.model.dto.res.SalesReportGoodsResVo;
import com.sckw.contract.model.vo.req.IdReqVo;
import com.sckw.contract.model.vo.req.QueryListReqVo;
import com.sckw.contract.api.model.dto.res.TradeEntInfoResVo;
import com.sckw.contract.service.operateService.KwcContractTradeService;
import com.sckw.core.model.enums.EntTypeEnum;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.core.web.context.LoginEntHolder;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.core.web.response.BaseResult;
import com.sckw.core.web.response.HttpResult;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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 java.util.Collections;
import java.util.List;

/**
 * @author czh
 * @desc 采购合同
 * @date 2023/7/13
 */
@RestController
@RequestMapping("kwcPurchase")
public class KwcPurchaseController {

    @Autowired
    private KwcContractTradeService kwcContractTradeService;

    /**
     * @desc: 分页查询
     * @param: reqVo 分页入参
     * @author: sky
     * @date 2023/7/17
     * @return HttpResult
     */
    @RequestMapping("queryListByPage")
    public HttpResult queryListByPage(@Valid @RequestBody QueryListReqVo reqVo) {
        reqVo.setEntType(EntTypeEnum.PURCHASER.getCode());
        return HttpResult.ok(kwcContractTradeService.queryListByPage(reqVo));
    }

    @PostMapping("queryTradeEntList")
    public BaseResult<List<TradeEntInfoResVo>> queryTradeEntList(@RequestBody TradeEntListQueryFeignDto queryFeignDto) {
        if(queryFeignDto.getCurEntId() == null){
            queryFeignDto.setCurEntId(LoginUserHolder.getEntId());
        }
        if(queryFeignDto.getTradeEntType() == null){
            String entTypes = LoginEntHolder.get().getEntTypes();
            if (entTypes.contains(String.valueOf(EntTypeEnum.SUPPLIER.getCode()))) {
                queryFeignDto.setTradeEntType(EntTypeEnum.PURCHASER.getCode());
            } else if (entTypes.contains(String.valueOf(EntTypeEnum.PURCHASER.getCode()))) {
                queryFeignDto.setTradeEntType(EntTypeEnum.SUPPLIER.getCode());
            } else {
                return BaseResult.success(Collections.emptyList());
            }
        }
        return BaseResult.success(kwcContractTradeService.queryTradeEntList(queryFeignDto));
    }

    /**
     * 查询销售报表商品列表
     * 范围为与当前公司存在合同（含已结束）的所有商品
     *
     * @param queryDto 查询参数
     * @return 商品列表
     */
    @PostMapping("/goodsList")
    public BaseResult<List<SalesReportGoodsResVo>> queryGoodsList(@RequestBody SalesReportGoodsQueryDto queryDto) {
        if(queryDto.getCurEntId() == null){
            queryDto.setCurEntId(LoginUserHolder.getEntId());
        }
        return BaseResult.success(kwcContractTradeService.querySalesReportGoodsList(queryDto));
    }

    /**
     * @desc: 合同详情
     * @param reqVo id
     * @author: czh
     * @date 2023/7/16
     * @return HttpResult
     */
    @PostMapping("detail")
    public HttpResult detail(@Valid @RequestBody IdReqVo reqVo) {
        return HttpResult.ok(kwcContractTradeService.detail(reqVo.getId()));
    }


    /**
     * @desc: 导出
     * @param reqVo 查询入参
     * @author: czh
     * @date 2023/7/17
     */
    @PostMapping(value = "export", produces = MediaType.APPLICATION_JSON_VALUE)
    public void export(@Valid @RequestBody QueryListReqVo reqVo, HttpServletResponse response) {
        reqVo.setEntType(EntTypeEnum.PURCHASER.getCode());
        kwcContractTradeService.export(reqVo, response);
    }

    /**
     * @return HttpResult
     * @desc: 分组查询
     * @author: czh
     * @date: 2023/7/18
     */
    @PostMapping("findListGroup")
    public HttpResult findListGroup(@RequestBody QueryListReqVo reqVo) {
        reqVo.setEntType(EntTypeEnum.PURCHASER.getCode());
        return HttpResult.ok(kwcContractTradeService.findListGroup(reqVo));
    }

}
