package com.inkasso.factoring.purchManagement.web;


import com.inkasso.adapter.util.CollectionUtil;
import com.inkasso.factoring.common.entity.BusinessHistoricDetailVo;
import com.inkasso.factoring.common.entity.ResponseJson;
import com.inkasso.factoring.purchManagement.entity.*;
import com.inkasso.factoring.purchManagement.entity.vo.PurchContractChangeVo;
import com.inkasso.factoring.purchManagement.entity.vo.PurchContractVo;
import com.inkasso.factoring.purchManagement.entity.vo.SearchGoods;
import com.inkasso.factoring.purchManagement.service.*;
import com.inkasso.factoring.salesManagement.entity.SearchSalesBasic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 采购合同管理表 前端控制器
 * </p>
 *
 * @author yafan
 * @since 2023-12-13
 */
@RestController
@RequestMapping("/purchManagement")
public class PurchContractController {

    @Autowired
    private ICPurchContractManagementService purchContractService;
    @Autowired
    private ICPurchContractSignConclusionService purchContractSignConclusionService;
    @Autowired
    private ICPurchContractSupplementInfoService purchContractSupplementInfoService;
    @Autowired
    private IPurchSupplementContractSignConclusionService purchSupplementContractSignConclusionService;
    @Autowired
    private ICPurchContractBasicInfoService purchContractBasicInfoService;
    @Autowired
    private RestTemplate restTemplate;
    @Value("${trade.purchContractChangeSignGetViewUrl}")
    private String purchContractChangeSignGetViewUrl;
    @Value("${trade.purchContractSignViewUrl}")
    private String purchContractSignViewUrl;

    /**
     * 根据业务编号查询质保金信息
     *
     * @param businessNumber
     * @return
     */
    @GetMapping("/getRetention")
    public ResponseJson getRetentionByBusinessNumber(String businessNumber) {
        return new ResponseJson(purchContractService.getRetentionByBusinessNumber(businessNumber));
    }


    /**
     * 根据业务编号查询已经完成的采购合同
     *
     * @return
     */
    @GetMapping("/getPurchCompleted")
    public ResponseJson getPurchCompleted(String businessNumber) {
        return new ResponseJson(purchContractService.getCompletedPurchContractByBusinessNumber(businessNumber));
    }

    /**
     * 列表
     *
     * @param searchSalesBasic
     * @return
     */
    @GetMapping("/list")
    public ResponseJson list(SearchSalesBasic searchSalesBasic) {
        return new ResponseJson(purchContractService.list(searchSalesBasic));
    }

    /**
     * 获取详情
     *
     * @param id
     * @return
     */
    @GetMapping("/create")
    public ResponseJson create(Long id) {
        return new ResponseJson(purchContractService.add(id));
    }

    /**
     * 选择货物
     *
     * @param searchGoods
     * @return
     */
    @PostMapping("/selectGoods")
    public ResponseJson selectGoods(@RequestBody SearchGoods searchGoods) {
        return new ResponseJson(purchContractService.selectGoods(searchGoods));
    }

    /**
     * 计算货物信息
     *
     * @param searchGoods 查询Vo
     * @return
     */
    @PostMapping("/calculateGoods")
    @ResponseBody
    public ResponseJson calculateGoods(@RequestBody SearchGoods searchGoods) {
        return new ResponseJson(purchContractService.calculateGoods(searchGoods));
    }


    /**
     * 采购合同录入
     *
     * @param purchContractVo
     * @return
     */
    @PostMapping("/doCreate")
    public ResponseJson doCreate(@RequestBody PurchContractVo purchContractVo) {
        purchContractService.doAdd(purchContractVo);
        return new ResponseJson("操作成功！");
    }

    /**
     * 下载
     *
     * @return
     */
    @GetMapping("/downloadPurchFile")
    public ResponseJson getView(Long id, HttpServletResponse response) {
        purchContractService.downloadFile(id,response);
        return new ResponseJson("下载成功！");
    }


}
