package com.malk.kuaikeli.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Strings;
import com.malk.kuaikeli.service.KKLService;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.server.common.McR;
import com.malk.utils.PublicUtil;
import com.malk.utils.UtilMap;
import com.malk.utils.UtilServlet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 错误抛出与拦截详见 CatchException
 */
@Slf4j
@RestController
@RequestMapping()
public class KKLController {

    @Autowired
    private KKLService kklService;

    /**
     * [动态] 发起单据, 分供应商进行流程推送
     */
    @PostMapping("supplier")
    McR supplier(HttpServletRequest request) {

        Map data = UtilServlet.getParamMap(request);
        log.info("supplier, {}", JSON.toJSONString(data));

        kklService.matchSupplier(data);
        return McR.success();
    }

    /**
     * 菜单计划, 编辑校验
     * -
     * 前端提交动作: 校验是否审批通过, 是否当日之前计划, 是否当日且在16点及以后时间, 均不允许提交
     * 后端校验返回、加任何产品都要走审批，减少20%以内直接过，每一个原材料
     */
    @PostMapping("validate")
    McR validate(@RequestBody Map data) {

        log.info("validate, {}", JSON.toJSONString(data));

        boolean approve=kklService.validateApprove(data);
        //09.27 改为通过业务关联规则更新菜单计划状态
        /*if(approve){
            kklService.updateState(UtilMap.getString(data,"poid"));
        }*/
        return McR.success();
    }

    /**
     * 菜单计划审批
     * -
     * 通过：数据更新到切配工作单及计划采购单
     * 拒绝：回退数据到菜单计划
     */
    @PostMapping("planApproval")
    McR planApproval(@RequestBody Map data) {
        log.info("planApproval, {}", JSON.toJSONString(data));
        if(PublicUtil.isNull(data,"fid","backFid","results","userId")){
            return McR.errorNullPointer();
        }
        kklService.planApproval(data);
        return McR.success();
    }

    /**
     * 定价单: 项目点 & 供应商大类, 数据匹配
     */
    @PostMapping("service")
    McR service(HttpServletRequest request) {

        Map data = UtilServlet.getParamMap(request);
        log.info("service, {}", JSON.toJSONString(data));

        kklService.serviceRate(data);
        return McR.success();
    }

    /**
     * 物料档案: 价格调整
     */
    @PostMapping("adjust")
    McR adjust(HttpServletRequest request) {

        Map data = UtilServlet.getParamMap(request);
        log.info("adjust, {}", JSON.toJSONString(data));

        kklService.adjustCost(data);
        return McR.success();
    }

    /**
     * 供应商原材料档案: 新增匹配
     * 更新定价单
     */
    @PostMapping("addition")
    McR addition(HttpServletRequest request) {

        Map data = UtilServlet.getParamMap(request);
        log.info("addition, {}", JSON.toJSONString(data));

        kklService.adjustCost(data);
        return McR.success();
    }


    /***
     * 新增定价单
     * @param request
     * @return
     */
    @PostMapping("updateDjd")
    McR updateDjd(HttpServletRequest request) {

        Map data = UtilServlet.getParamMap(request);
        log.info("addition, {}", JSON.toJSONString(data));

        kklService.additionMaterial(data);
        return McR.success();
    }


    /**
     * 周日同步更新价格
     */
    @PostMapping("sync")
    McR sync() {
        log.info("sync price");
        kklService.syncPrice();
        return McR.success();
    }

    /**
     * 获取新老供应商差异匹配（供应商原材料全量匹配）
     * @param oldSupplierCode
     * @param newSupplierCode
     * @return
     */
    @GetMapping("getDiffMatchingSuppliers")
    McR getDiffMatchingSuppliers(String oldSupplierCode,String newSupplierCode){
        if (Strings.isNullOrEmpty(oldSupplierCode) || Strings.isNullOrEmpty(newSupplierCode)){
            return McR.errorParam("参数缺失");
        }
        List<Map<String,String>> result = kklService.getDiffMatchingSuppliers(oldSupplierCode,newSupplierCode);
        return McR.success(result);
    }

    /**
     * 获取新老供应商差异匹配（定价单明细中间表匹配）
     * @param projectCode
     * @param oldSupplierCode
     * @param newSupplierCode
     * @return
     */
    @GetMapping("getDiffMatchingSuppliers2")
    McR getDiffMatchingSuppliers2(String projectCode,String oldSupplierCode,String newSupplierCode){
        if (Strings.isNullOrEmpty(projectCode) || Strings.isNullOrEmpty(oldSupplierCode) || Strings.isNullOrEmpty(newSupplierCode)){
            return McR.errorParam("参数缺失");
        }
        List<Map<String,String>> result = kklService.getDiffMatchingSuppliers2(projectCode,oldSupplierCode,newSupplierCode);
        return McR.success(result);
    }

    /**
     * 更新定价单供应商
     * @param map
     * @return
     */
    @PostMapping("updateSuppliers")
    McR updateSuppliers(@RequestBody Map map) {
        String formInstId = String.valueOf(map.get("formInstId"));
        if (Strings.isNullOrEmpty(formInstId)){
            return McR.errorParam("参数缺失");
        }
        kklService.updateSuppliers(formInstId);
        return McR.success();
    }

    @PostMapping("test")
    McR test(HttpServletRequest request) {
        Map data = UtilServlet.getParamMap(request);
        log.info("xxxx, {}", data);
        return McR.success();
    }

    /***
     * 采购确认提交后数据处理
     * @return
     */
    @PostMapping("purchase")
    McR purchaseConfirmation(@RequestBody Map map){
        String formInstId = String.valueOf(map.get("formInstId"));
        if (Strings.isNullOrEmpty(formInstId)){
            return McR.errorParam("参数缺失");
        }
        kklService.purchaseConfirmation(formInstId);
        return McR.success();
    }

}
