package com.jwipc.order.controller.pc;

import com.jwipc.exception.DaoException;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.order.dto.PcnChangeApplyListDTO;
import com.jwipc.order.dto.PcnChangeSubjectDTO;
import com.jwipc.order.logic.PcnManagerLogic;
import com.jwipc.order.logic.PurchaseOrderLogic;
import com.jwipc.security.LoginUtil;
import com.jwipc.security.dto.CurrentUserDTO;
import com.jwipc.supplier.client.SupplierClient;
import com.jwipc.sys.client.UserClient;
import com.jwipc.sys.client.dto.UserDTO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

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

/**
 * 功能描述:
 *
 * @Author: zx
 * @Date: 2024/3/19 0019 10:22
 */
@Controller
@RequestMapping(MODULE_ROOT + "/pcn/page")
@RequiredArgsConstructor
public class PcnManagerPageController {

    @Autowired
    PcnManagerLogic pcnManagerLogic;
    @Autowired
    PurchaseOrderLogic purchaseOrderLogic;
    @Autowired
    SupplierClient supplietClient;
    @Autowired
    UserClient userClient;

    /**
     * PCN变更管理
     * @param type  0非外协 1外协
     * @param model
     * @return
     */
    @RequestMapping("/pcnManagerPage")
    public String pcnManagerPage(Model model) {
        model.addAttribute("viewType", 0);
        return MODULE_ROOT + "/pcn/pcn_manager_list";
    }


    @RequestMapping("/pcnAcceptPage")
    public String pcnAcceptPage(String id, Model model) {
        model.addAttribute("id", id);
        model.addAttribute("viewType", 0);
        return MODULE_ROOT + "/pcn/pcn_manager_accept";
    }


    @RequestMapping("/pcnAgreePage")
    public String pcnAgreePage(String id, Model model) {
        model.addAttribute("id", id);
        model.addAttribute("viewType", 0);
        return MODULE_ROOT + "/pcn/pcn_manager_agree";
    }


    /**
     * @param viewType 0非外协 1外协',
     * @param model
     * @return
     */
    @RequestMapping("/addpage")
    public String addPcnPage( Model model) {
        if (LoginUtil.isSupplier()) {
            CurrentUserDTO currentUserDTO = LoginUtil.getCurrentUser();
            model.addAttribute("supplierCode", currentUserDTO.getSupplierCode());
            model.addAttribute("supplierName", currentUserDTO.getSupplierName());
            //获取供应商邀请人登录账号
            String recommender = pcnManagerLogic.getRecommander(currentUserDTO.getSupplierId());
            //根据邀请人账号设置邀请人登录名称
            UserDTO userDTO = userClient.getByAccount(recommender);
            String purchaseUserName = userDTO.getUsername();
//            String purchaseUserName = pcnManagerLogic.getPurchaseUserName(recommender);
            String purchaseGroupCode = pcnManagerLogic.getSupPgCode(currentUserDTO.getSupplierCode());
            Long purchaseGroupId = purchaseOrderLogic.gerPurchaseIdByCode(purchaseGroupCode);
            model.addAttribute("purchaseGroupCode", purchaseGroupCode);
            model.addAttribute("purchaseUserName", purchaseUserName);
            model.addAttribute("recommender", recommender);
            model.addAttribute("purchaseGroupId", purchaseGroupId);
            String supplierFlag = "1";
            model.addAttribute("supplierFlag", supplierFlag);
        } else {
            model.addAttribute("supplierCode", null);
            model.addAttribute("supplierName", null);
        }
        model.addAttribute("viewType", 0);
        return MODULE_ROOT + "/pcn/pcn_manager_add";
    }


    /**
     * pcn编辑页面
     * @param id
     * @param model
     * @param viewType 页面类型 空为普通的pcn变更 ，1为外协pCN变更
     * @return
     */
    @RequestMapping("/editPcn")
    public String editPcn(String id, Model model) {
        if (StringUtils.isEmpty(id)) {
            throw new DaoException("查看详情传递参数异常");
        }
        if (LoginUtil.isSupplier()) {
            CurrentUserDTO currentUserDTO = LoginUtil.getCurrentUser();
            model.addAttribute("supplierCode", currentUserDTO.getSupplierCode());
            model.addAttribute("supplierName", currentUserDTO.getSupplierName());
        } else {
            model.addAttribute("supplierCode", null);
            model.addAttribute("supplierName", null);
        }
        PcnChangeApplyListDTO pcnChangeApplyListVo = pcnManagerLogic.selectPcnMangeById(id);
        model.addAttribute("pcnChangeApplyList", pcnChangeApplyListVo);
        List<PcnChangeSubjectDTO> pcnChangeSubjectVos = pcnManagerLogic.selectRecordByApplayId(id);
        model.addAttribute("pcnChangeSubjectList", pcnChangeSubjectVos);
        model.addAttribute("viewType", 0);
        return MODULE_ROOT + "/pcn/pcn_manager_edit";
    }

    /**
     * 查看详情
     * @param id
     * @param model
     * @param viewType 0非外协 1外协',
     * @return
     */
    @AuditLogInter(value = "查看详情数据", module = "PCN变更申请")
    @RequestMapping("/viewDetail")
    public String viewDetail(String id, Model model) {
        if (StringUtils.isEmpty(id)) {
            throw new DaoException("查看详情传递参数异常");
        }
        PcnChangeApplyListDTO pcnChangeApplyListVo = pcnManagerLogic.selectPcnMangeById(id);
        model.addAttribute("pcnChangeApplyList", pcnChangeApplyListVo);
        List<PcnChangeSubjectDTO> pcnChangeSubjectVos = pcnManagerLogic.selectRecordByApplayId(id);
        model.addAttribute("pcnChangeSubjectList", pcnChangeSubjectVos);
        model.addAttribute("viewType", 0);
        return MODULE_ROOT + "/pcn/pcn_manager_detail";
    }

    /**
     * sqe审批
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/sqeExamine")
    public String sqeDetail(String id, Model model) {
        if (StringUtils.isEmpty(id)) {
            throw new DaoException("查看详情传递参数异常");
        }
        PcnChangeApplyListDTO pcnChangeApplyListVo = pcnManagerLogic.selectPcnMangeById(id);
        model.addAttribute("pcnChangeApplyList", pcnChangeApplyListVo);
        List<PcnChangeSubjectDTO> pcnChangeSubjectVos = pcnManagerLogic.selectRecordByApplayId(id);
        model.addAttribute("pcnChangeSubjectList", pcnChangeSubjectVos);
        model.addAttribute("viewType", 0);
        return MODULE_ROOT + "/pcn/pcn_manager_examine";
    }
}
