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 com.jwipc.inquiry.dto.InquiryDTO;
import com.jwipc.inquiry.dto.PriceInfoAuditDTO;
import com.jwipc.inquiry.dto.PriceInfoDTO;
import com.jwipc.inquiry.logic.PriceInfoAuditLogic;
import com.jwipc.inquiry.logic.PriceInfoLogic;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.client.UserClient;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.List;

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

/**
 * <p>
 * 调价管理表 前端控制器
 * </p>
 *
 * @author shenyang.ou
 * @since 2024-03-08
 */
@Controller
@RequestMapping(MODULE_ROOT + "/price/audit")
@RequiredArgsConstructor
public class PriceInfoAuditPageController {
    private final PriceInfoLogic priceInfoLogic;
    private final PriceInfoAuditLogic priceInfoAuditLogic;
    private final UserClient userClient;
    @GetMapping("/index")
    public String index(Model model) {
        model.addAttribute("userName", LoginUtil.getName());
        return MODULE_ROOT + "/audit/list";
    }
    @AuditLogInter(value = "新增/编辑查看信息", module = "调价管理")
    @GetMapping("/detail")
    public String detail(Model model, Long id,Integer optType,String priceIds) {
        PriceInfoAuditDTO priceInfoAuditDTO = new PriceInfoAuditDTO();
        if (ObjectUtil.isNotNull(id)){
            priceInfoAuditDTO = priceInfoAuditLogic.getById(id);
        }
        if (optType==3){
            List<String> priceIdList = Arrays.asList(StringUtils.split(priceIds, ","));
            PriceInfoDTO priceInfoDTO = priceInfoLogic.getById(Long.valueOf(priceIdList.get(0)));
            if (ObjectUtil.isNotEmpty(priceInfoDTO)){
                priceInfoAuditDTO.setSupplierCode(priceInfoDTO.getSupplierCode());
                priceInfoAuditDTO.setSupplierName(priceInfoDTO.getSupplierName());
                priceInfoAuditDTO.setTaxCode(priceInfoDTO.getTaxCode());
                priceInfoAuditDTO.setCurrencyCode(priceInfoDTO.getCurrencyCode());
                priceInfoAuditDTO.setPaymentMethod(priceInfoDTO.getPaymentMethod());
                priceInfoAuditDTO.setPurchaseOrgCode(priceInfoDTO.getPurchaseOrgCode());
                priceInfoAuditDTO.setCompanyCode(priceInfoDTO.getCompanyCode());
                priceInfoAuditDTO.setPurchaseGroupCode(priceInfoDTO.getPurchaseGroupCode());

                priceInfoAuditDTO.setPurchaseCategoryCode(priceInfoDTO.getPurchaseCategoryCode());
                priceInfoAuditDTO.setSpotFlag(priceInfoDTO.getSpotFlag()==null?0:priceInfoDTO.getSpotFlag());
                priceInfoAuditDTO.setBusinessAgreeFlag(priceInfoDTO.getBusinessAgreeFlag());
                priceInfoAuditDTO.setCustomerBearFlag(priceInfoDTO.getCustomerBearFlag());
                priceInfoAuditDTO.setBearRatio(priceInfoDTO.getBearRatio());

            }
        }
        priceInfoAuditDTO.setSpotFlag(priceInfoAuditDTO.getSpotFlag()==null?0:priceInfoAuditDTO.getSpotFlag());
        model.addAttribute("data", priceInfoAuditDTO);
        model.addAttribute("optType", optType);
        model.addAttribute("lookFlag", optType==2);
        model.addAttribute("priceIds", priceIds);
        return MODULE_ROOT + "/audit/detail";
    }

    @GetMapping("/look")
    public String look(Model model, String id) {
        //无密码登录
//        userClient.noPasswordLogin(1L);
        if(!StpUtil.isLogin()){
            StpUtil.login(-99999,30);
        }
        AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, "0CoJUm6Qyw8W8jud".getBytes(), "0102030405060708".getBytes());
        String idStr = aes.decryptStr(id, CharsetUtil.CHARSET_UTF_8);
        PriceInfoAuditDTO priceInfoAuditDTO = priceInfoAuditLogic.getById(Long.valueOf(idStr));
        model.addAttribute("data", priceInfoAuditDTO);
        model.addAttribute("optType", 2);
        model.addAttribute("lookFlag", true);
        model.addAttribute("priceIds", "");
        return MODULE_ROOT + "/audit/detail";
    }
}
