package com.jwipc.inquiry.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.jwipc.inquiry.dto.AssistantMaterialSupplierDTO;
import com.jwipc.inquiry.dto.AssistantSupplierDTO;
import com.jwipc.inquiry.dto.InquiryDTO;
import com.jwipc.inquiry.dto.InquiryMaterialSupplierDTO;
import com.jwipc.inquiry.logic.InquiryLogic;
import com.jwipc.inquiry.logic.InquiryMaterialSupplierLogic;
import com.jwipc.inquiry.model.query.InquiryQuoteQuery;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.client.CompanyClient;
import com.jwipc.sys.client.UserClient;
import com.jwipc.sys.client.dto.CompanyDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

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

/**
 * @author shenyang.ou
 * @date 2024/2/4 19:48
 * @desc 询价定价
 */
@Controller
@RequestMapping(MODULE_ROOT+ "/fix")
@RequiredArgsConstructor
public class InquiryFixPageController {
    private final InquiryLogic inquiryLogic;
    private final CompanyClient companyClient;
    private final UserClient userClient;
    private final InquiryMaterialSupplierLogic inquiryMaterialSupplierLogic;

    @GetMapping("/index")
    public String index(Model model,Integer type) {
        model.addAttribute("userName", LoginUtil.getName());
        model.addAttribute("type", type);
        return MODULE_ROOT + "/fix/list";
    }

    @GetMapping("/report/index")
    public String reportIndex(Model model) {
        return MODULE_ROOT + "/fix/report_list";
    }

    /**
     * 定价页面
     * @param id 询价页面
     * @param optType 操作类型；0-查看；1-定价；2-大数据定价；3-商务定价；4-价格确认
     */
    @GetMapping("/fixPrice")
    @AuditLogInter(module = "比价定价",value = "查看-比价定价议价详情页面")
    public String fixPrice(Model model, Long id,Integer optType) {
        InquiryDTO inquiryDTO =inquiryLogic.getById(id);
        model.addAttribute("data", inquiryDTO);
        model.addAttribute("optType", optType);
        model.addAttribute("lookFlag", optType==0||optType==4);
        return MODULE_ROOT + "/fix/fix_price";
    }

    /**
     * 定价查看页面
     * @param id 询价页面
     */
    @GetMapping("/fixPrice/look")
    public String fixPriceLook(Model model, String id) {
        //无密码登录
//        userClient.noPasswordLogin(1L);
        if(!StpUtil.isLogin()){
            StpUtil.login(-99999,30);
        }
        //构建AES
        AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, "0CoJUm6Qyw8W8jud".getBytes(), "0102030405060708".getBytes());
        String idStr = aes.decryptStr(id, CharsetUtil.CHARSET_UTF_8);
        InquiryDTO inquiryDTO = inquiryLogic.getById(Long.valueOf(idStr));
        model.addAttribute("data", inquiryDTO);
        model.addAttribute("optType", 0);
        model.addAttribute("lookFlag", true);
        return MODULE_ROOT + "/fix/fix_price";
    }

    /**
     * 比价小助手
     */
    @GetMapping("/assistant")
    @AuditLogInter(module = "比价定价",value = "详情页-比价小助手按钮")
    public String assistant(Model model, Long inquiryId) {
        InquiryDTO inquiryDTO =inquiryLogic.getById(inquiryId);
        CompanyDTO companyDTO = companyClient.getByCode(inquiryDTO.getCompanyCode());
        if (ObjectUtil.isNotNull(companyDTO)){
            model.addAttribute("companyName", companyDTO.getCompanyName());
        }
        model.addAttribute("inquiryId", inquiryId);
        model.addAttribute("data", inquiryDTO);
        List<AssistantSupplierDTO> assistantSupplierDTOS = inquiryMaterialSupplierLogic.getAssistantSupplierByInquiryId(inquiryId);
        Set<String> supplierCodes = assistantSupplierDTOS.stream().collect(Collectors.groupingBy(AssistantSupplierDTO::getSupplierCode)).keySet();
        List<AssistantMaterialSupplierDTO> assistantMaterialSupplier = inquiryMaterialSupplierLogic.getAssistantMaterialSupplier(inquiryId,supplierCodes);

        model.addAttribute("firstMaterialCode", assistantMaterialSupplier.get(0).getMaterialCode());
        model.addAttribute("supplierInfoFormMaps", assistantSupplierDTOS);
        model.addAttribute("materialInfoList", assistantMaterialSupplier);
        model.addAttribute("supplierInfoFormMapsSize", assistantSupplierDTOS.size() * 2);
        model.addAttribute("supplierInfoFormMapsWithSize", assistantSupplierDTOS.size() * 400 + 800);
        return MODULE_ROOT + "/fix/assistant";
    }
}
