package com.sunxung.factoring.web.supplier;

import com.sunxung.factoring.entity.GridPage;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.entprise.Enterprise;
import com.sunxung.factoring.entity.project.vo.SearchBusinessProcessingTask;
import com.sunxung.factoring.entity.supplier.*;
import com.sunxung.factoring.service.entprise.IEnterpriseService;
import com.sunxung.factoring.service.supplier.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author liutao
 * @date 2023-12-04
 */
@RestController
public class SupplierController {

    @Resource(name = "SupplierInfoService")
    private ISupplierInfoService supplierInfoService;

    @Autowired
    private IDueDiligenceAllocationService dueDiligenceAllocationService;

    @Autowired
    private ISupplierApprovalFirstService supplierApprovalFirstService;

    @Autowired
    private ISupplierApprovalSecondService supplierApprovalSecondService;

    @Autowired
    private IDueDiligenceAppointmentService dueDiligenceAppointmentService;

    @Autowired
    private IConfirmationDueDiligenceTimeService confirmationDueDiligenceTimeService;

    @Autowired
    private IVideoDueDiligenceService videoDueDiligenceService;

    @Autowired
    private IEnterpriseService enterpriseService;
    /**
     * 开启流程
     * @param supplierInfo
     * @return
     */
    @RequestMapping("api/supplier/startProcess")
    public ResponseJson startProcess(@RequestBody SupplierInfo supplierInfo){
        String status = supplierInfoService.startProcess(supplierInfo);
        return new ResponseJson(status);
    }

    /**
     * 根据条件查询数据
     * @param search
     * @return
     */
    @RequestMapping("supplier/getAll")
    public GridPage getAll(@RequestBody SearchBusinessProcessingTask search){
        GridPage<SupplierInfo> page = supplierInfoService.getBySearch(search);
        return page;
    }

    /**
     * 获取尽调分配信息
     * @param id
     * @return
     */
    @RequestMapping("supplier/getDueDiligenceAllocation")
    public ResponseJson getDueDiligenceAllocation(@RequestParam Long id){
        DueDiligenceAllocation dueDiligenceAllocation = dueDiligenceAllocationService.getDueDiligenceAllocation(id);
        return new ResponseJson(dueDiligenceAllocation);
    }

    /**
     * 尽调分配
     * @param dueDiligenceAllocation
     * @return
     */
    @RequestMapping("supplier/dueDiligenceAllocation")
    public ResponseJson dueDiligenceAllocation(@RequestBody DueDiligenceAllocation dueDiligenceAllocation){
        SupplierInfo supplierInfo = supplierInfoService.get(dueDiligenceAllocation.getcSupplierInfoId());
        Enterprise enterprise = enterpriseService.getById(supplierInfo.getcEnterpriseId());
        supplierInfo.setEnterprise(enterprise);
        String status = dueDiligenceAllocationService.addInfo(dueDiligenceAllocation, supplierInfo);
        supplierInfo.setStatusStr(status);
        supplierInfoService.updateById(supplierInfo);
        return new ResponseJson();
    }

    /**
     * 获取第一步审核信息
     * @param id
     * @return
     */
    @RequestMapping("supplier/getFirstReviewById")
    public ResponseJson getFirstReviewById(@RequestParam Long id){
        SupplierInfo supplierInfo = supplierApprovalFirstService.getFirstReviewById(id);
        return new ResponseJson(supplierInfo);
    }

    /**
     * 审核第一步测算
     * @return
     */
    @RequestMapping("supplier/firstReview/measurement")
    public ResponseJson firstMeasurement(@RequestBody SupplierInfo supplierInfo){
        SupplierApprovalFirst measurement = supplierApprovalFirstService.measurement(supplierInfo);
        return new ResponseJson(measurement);
    }

    /**
     * 审核第一步
     * @return
     */
    @RequestMapping("supplier/firstReview")
    public ResponseJson firstReview(@RequestBody SupplierInfo supplierInfo){
        supplierApprovalFirstService.firstReview(supplierInfo);
        return new ResponseJson();
    }

    /**
     * 获取第二步审核信息
     * @param id
     * @return
     */
    @RequestMapping("supplier/getSecondReviewById")
    public ResponseJson getSecondReviewById(@RequestParam Long id){
        SupplierApprovalSecond supplierApprovalSecond = supplierApprovalSecondService.getSecondReviewById(id);
        return new ResponseJson(supplierApprovalSecond);
    }

    /**
     * 第二步审核测算打分
     * @param supplierApprovalSecond
     * @return
     */
    @RequestMapping("supplier/secondReview/measurement")
    public ResponseJson secondMeasurement(@RequestBody SupplierApprovalSecond supplierApprovalSecond){
        SupplierApprovalSecond result = supplierApprovalSecondService.measurement(supplierApprovalSecond);
        return new ResponseJson(result);
    }

    /**
     * 第二步审核
     * @param supplierApprovalSecond
     * @return
     */
    @RequestMapping("supplier/secondReview")
    public ResponseJson secondReview(@RequestBody SupplierApprovalSecond supplierApprovalSecond){
        supplierApprovalSecondService.secondReview(supplierApprovalSecond);
        return new ResponseJson();
    }

    /**
     * c端完成尽调预约
     * @param id
     * @param historyId
     * @return
     */
    @RequestMapping("api/supplier/completeVideoDueDiligenceAppointment")
    public ResponseJson completeVideoDueDiligenceAppointment(@RequestParam Long id, @RequestParam Long historyId){
        dueDiligenceAppointmentService.completeDueDiligenceAppointment(id,historyId);
        return new ResponseJson();
    }

    /**
     * 获取尽调时间确认信息
     * @return
     */
    @RequestMapping("supplier/getDueDiligenceConfirm")
    public ResponseJson getDueDiligenceConfirm(@RequestParam Long supplierId){
        ConfirmationDueDiligenceTime confirmationDueDiligenceTime = confirmationDueDiligenceTimeService.getDueDiligenceConfirm(supplierId);
        return new ResponseJson(confirmationDueDiligenceTime);
    }

    /**
     * 尽调时间确认
     * @return
     */
    @RequestMapping("supplier/dueDiligenceConfirm")
    public ResponseJson dueDiligenceConfirm(@RequestBody ConfirmationDueDiligenceTime confirmationDueDiligenceTime){
        confirmationDueDiligenceTimeService.dueDiligenceConfirm(confirmationDueDiligenceTime);
        return new ResponseJson();
    }

    /**
     * 获取视频尽调信息
     * @return
     */
    @RequestMapping("supplier/getVideoDueDiligence")
    public ResponseJson getVideoDueDiligence(@RequestParam Long supplierId){
        VideoDueDiligence videoDueDiligence = videoDueDiligenceService.getVideoDueDiligence(supplierId);
        return new ResponseJson(videoDueDiligence);
    }

    /**
     * 视频尽调录入
     * @return
     */
    @RequestMapping("supplier/videoDueDiligence")
    public ResponseJson videoDueDiligence(@RequestBody VideoDueDiligence videoDueDiligence){
        videoDueDiligenceService.videoDueDiligence(videoDueDiligence);
        return new ResponseJson();
    }

    /**
     * 视频尽调测算打分
     * @param videoDueDiligence
     * @return
     */
    @RequestMapping("supplier/video/measurement")
    public ResponseJson videoMeasurement(@RequestBody VideoDueDiligence videoDueDiligence){
        VideoDueDiligence result = videoDueDiligenceService.measurement(videoDueDiligence);
        return new ResponseJson(result);
    }

    /**
     * 供应商准入报告
     * @param supplierId
     * @return
     */
    @RequestMapping("supplier/report")
    public ResponseJson report(@RequestParam Long supplierId){
        VideoDueDiligence videoDueDiligence = supplierInfoService.report(supplierId);
        return new ResponseJson(videoDueDiligence);
    }

    /**
     * 供应商准入报告
     * @param supplierId
     * @return
     */
    @RequestMapping("/supplier/download/report")
    public void downloadReport(@RequestParam Long supplierId, HttpServletResponse response) throws IOException {
        supplierInfoService.downloadReport(supplierId,response);
    }
}
