package com.jwipc.inquiry.controller.rest;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.jwipc.inquiry.dto.InquiryMaterialDTO;
import com.jwipc.inquiry.dto.InquirySupplierImportDTO;
import com.jwipc.inquiry.dto.PriceInfoDTO;
import com.jwipc.inquiry.logic.PriceInfoLogic;
import com.jwipc.inquiry.model.query.PriceInfoQuery;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

import static com.jwipc.inquiry.constant.ModuleConst.MODULE_ROOT;

/**
 * @author shenyang.ou
 * @date 2024/2/28 12:13
 * @desc
 */
@RestController
@RequestMapping(MODULE_ROOT + "/price/rest")
@RequiredArgsConstructor
public class PriceInfoRestController {
    private final PriceInfoLogic priceInfoLogic;
    /**
     * 列表
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public PageView list(PriceInfoQuery query) {
        query.setUserId(LoginUtil.getUserId());
        return priceInfoLogic.findByPage(query);
    }


    @RequestMapping("/chart")
    public ResponseEntity chart(@RequestParam(defaultValue = "0") Integer type, String materialCode, String supplierCode, String purchaseType) {
        PriceInfoQuery query = new PriceInfoQuery();
        query.setPurchaseType(purchaseType);
        query.setMaterialCode(materialCode);
        query.setSupplierCode(supplierCode);
        if (type == 2) {
            query.setStartFromTime(DateUtil.format(DateUtil.offsetMonth(new Date(),-12),"yyyy-MM-dd HH:mm:ss"));
            query.setStartToTime(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        } else if (type == 3) {
            query.setStartFromTime(DateUtil.format(DateUtil.offsetMonth(new Date(),-3),"yyyy-MM-dd HH:mm:ss"));
            query.setStartToTime(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        }
        return ResponseEntity.ok(priceInfoLogic.listAll(query));
    }

    /**
     * 更新状态
     * @param id 价格记录id
     * @param optType 操作类型；0-生效；1-失效；2-关闭
     */
    @PostMapping("/updateStatus")
    public ResponseEntity updateStatus(Long id,Integer optType,String invalidRemark) {
        priceInfoLogic.updateStatus(id,optType,invalidRemark);
        return ResponseEntity.ok(true);
    }

    /**
     * Dimension
     * @param exportFiled 导出的列信息
     * @param queryStr 查询条件
     */
    @PostMapping("/export")
    @AuditLogInter(module = "价格信息记录",value = "导出按钮")
    public void export(HttpServletResponse response, String exportFiled, String queryStr) throws IOException {
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        PriceInfoQuery query =  JSON.parseObject(queryStr,PriceInfoQuery.class);
        query.setUserId(LoginUtil.getUserId());
        final List<PriceInfoDTO> exportData = priceInfoLogic.listAll(query);
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,"价格信息记录-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN),"sheet", exportData, exportFiled);
    }

    /**
     * 导入
     * @param file 导入文件
     * @return
     */
    @PostMapping("import")
    public ResponseEntity<String> importData(@RequestParam(value = "file") MultipartFile file) {
        String message = priceInfoLogic.importData(file);
        return ResponseEntity.ok(message);
    }

    /**
     * 组装转询价物料列表
     *
     * @param priceInfoIds 价格信息记录ids
     * @return
     */
    @RequestMapping("/make/inquiry/material")
    public ResponseEntity<List<InquiryMaterialDTO>> makeInquiryMaterial(String priceInfoIds) {
        return ResponseEntity.ok(priceInfoLogic.makeInquiryMaterial(priceInfoIds));
    }
}
