package com.inkasso.factoring.creditmanagement.web;


import com.inkasso.adapter.interfaces.AvoidRepeatableCommit;
import com.inkasso.adapter.response.SuccessResponseData;
import com.inkasso.factoring.common.entity.ResponseJson;
import com.inkasso.factoring.creditmanagement.entity.DueDiligenceAppointment;
import com.inkasso.factoring.creditmanagement.entity.Enterprise;
import com.inkasso.factoring.creditmanagement.entity.SupplierContact;
import com.inkasso.factoring.creditmanagement.entity.SupplierInfo;
import com.inkasso.factoring.creditmanagement.service.IDueDiligenceAppointmentService;
import com.inkasso.factoring.creditmanagement.service.IEnterpriseService;
import com.inkasso.factoring.creditmanagement.service.ISupplierContactService;
import com.inkasso.factoring.creditmanagement.service.ISupplierInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Nullable;
import java.util.List;

/**
 * <p>
 * 供应商企业信息 前端控制器
 * </p>
 *
 * @author liutao
 * @since 2023-11-25
 */
@RestController
@RequestMapping("/supplierInfo")
public class CSupplierInfoController {

    @Autowired
    private ISupplierInfoService supplierInfoService;

    @Autowired
    private ISupplierContactService supplierContactService;

    @Autowired
    private IEnterpriseService enterpriseService;

    @Autowired
    private IDueDiligenceAppointmentService dueDiligenceAppointmentService;

    /**
     * 根据关键字模糊查询企业   企查查886高级接口
     */
    @RequestMapping("getBusinessByKeyWord")
    public ResponseJson getBusinessByKeyWord(@RequestParam String keyword){
        List<Enterprise> enterpriseList = enterpriseService.getBusinessByKeyWord(keyword);
        return new ResponseJson(enterpriseList);
    }

    /**
     * 新增供应商
     * @param supplierInfo
     * @return
     */
    @RequestMapping("addSupplier")
    @AvoidRepeatableCommit(key = "supplierInfo/addSupplier")
    public ResponseJson addSupplier(@Validated @RequestBody SupplierInfo supplierInfo){
        SupplierInfo result = supplierInfoService.addSupplier(supplierInfo);
        return new ResponseJson(result);
    }

    /**
     * 获取尽调预约信息
     * @param id
     * @return
     */
    @RequestMapping("getOfflineDueDiligenceAppointment")
    public ResponseJson getOfflineDueDiligenceAppointment(@RequestParam Long id){
        DueDiligenceAppointment dueDiligenceAppointment = dueDiligenceAppointmentService.getOfflineDueDiligenceAppointment(id);
        return new ResponseJson(dueDiligenceAppointment);
    }

    /**
     * 尽调预约
     * @param dueDiligenceAppointment
     * @return
     */
    @RequestMapping("offlineDueDiligenceAppointment")
    @AvoidRepeatableCommit(key = "supplierInfo/offlineDueDiligenceAppointment")
    public ResponseJson offlineDueDiligenceAppointment(@Validated @RequestBody DueDiligenceAppointment dueDiligenceAppointment){
        dueDiligenceAppointmentService.offlineDueDiligenceAppointment(dueDiligenceAppointment);
        return new ResponseJson();
    }

    /**
     * 建工企业  增加担保企业
     * @param supplierInfo
     * @return
     */
    @RequestMapping("addBusiness")
    @AvoidRepeatableCommit(key = "supplierInfo/addBusiness")
    public ResponseJson addBusiness(@RequestBody SupplierInfo supplierInfo){
        SupplierInfo result = supplierInfoService.addBusiness(supplierInfo);
        return new ResponseJson(result);
    }

    /**
     * 根据供应商id获取  供应商和担保企业信息补充信息页面信息
     * @param
     * @return
     */
    @RequestMapping("findSupplier")
    public ResponseJson findSupplierById(@RequestParam Long id){
        SupplierInfo supplierInfo = supplierInfoService.findSupplierById(id);
        return new ResponseJson(supplierInfo);
    }

    /**
     * 根据供应商id获取  非建工供应商
     * @param
     * @return
     */
    @RequestMapping("findNonConstructionIndustry")
    public ResponseJson findNotConstructionIndustrySupplierById(@RequestParam Long id){
        SupplierInfo supplierInfo = supplierInfoService.getById(id);
        Enterprise enterprise = enterpriseService.getById(supplierInfo.getcEnterpriseId());
        supplierInfo.setEnterprise(enterprise);
        SupplierContact supplierContact = supplierContactService.lambdaQuery().eq(SupplierContact::getSupplierInfoId, id).one();
        supplierInfo.setSupplierContact(supplierContact);
        return new ResponseJson(supplierInfo);
    }

    /**
     * 补充供应商信息
     * @param supplierInfo
     * @return
     */
    @RequestMapping("supplement")
    @AvoidRepeatableCommit(key = "supplierInfo/supplement")
    public ResponseJson supplement(@RequestBody SupplierInfo supplierInfo) {
        SupplierInfo supplement = supplierInfoService.supplement(supplierInfo);
        return new ResponseJson(supplement);
    }
     /**
     * 根据用户ID 查询 供应商信息
     * @param sysUserId 用户ID
     * @return
     */
    @RequestMapping("findSupplierInfo")
    @ResponseBody
    public Object findSupplierInfo(@Nullable @RequestParam Long sysUserId ){
        List<Enterprise> enterprises = enterpriseService.findSupplierInfo(sysUserId);
        return new SuccessResponseData(enterprises);
    }
    /**
     * 根据供应商ID 查询 核心企业信息
     * @param cSupplierId 供应商ID
     * @return
     */
    @RequestMapping("findEnterpriseInfo")
    @ResponseBody
    public Object findEnterpriseInfo(@Nullable @RequestParam Long cSupplierId ){
        List<Enterprise> enterprises = enterpriseService.findEnterpriseInfo(cSupplierId);
        return new SuccessResponseData(enterprises);
    }

    /**
     * 我的企业列表
     * @return
     */
    @RequestMapping("list")
    public ResponseJson list(){
        List<SupplierInfo> supplierInfos = supplierInfoService.allList();
        return new ResponseJson(supplierInfos);
    }
}
