package com.inkasso.factoring.steelwaiter;

import com.alibaba.fastjson.JSONArray;
import com.inkasso.adapter.util.CollectionUtil;
import com.inkasso.factoring.acceptanceManagement.entity.CAcceptanceGoodsInfo;
import com.inkasso.factoring.common.entity.ResponseJson;
import com.inkasso.factoring.file.entity.FileStorageDO;
import com.inkasso.factoring.steelwaiter.entity.SellBill;
import com.inkasso.factoring.steelwaiter.entity.SellBillItem;
import com.inkasso.factoring.steelwaiter.entity.vo.SearchVo;
import com.inkasso.factoring.steelwaiter.service.SteelWaiterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 钢小二
 *
 * @author liutao
 * @date 2023-10-26
 */
@Controller
@RequestMapping("steel/waiter")
public class SteelWaiterController {

    @Autowired
    private SteelWaiterService steelWaiterService;


    @Autowired
    private RestTemplate restTemplate;

    @Value("${trade.steelWaiterNumbers}")
    private String steelWaiterNumbers;

    @Value("${trade.steelWaiterGoods}")
    private String steelWaiterGoods;

    @ResponseBody
    @RequestMapping("billNumbers")
    public ResponseJson findAllBillNumbers(String supplierName, String keyword) {
//        List<String> list = steelWaiterService.findAllBillNumbers(supplierName,keyword);
//        return new ResponseJson(list);

        String url = steelWaiterNumbers + "?supplierName=" + supplierName+"&keyword="+keyword;
        String forObject = restTemplate.getForObject(url, String.class);
        JSONArray array = JSONArray.parseArray(forObject);
        return new ResponseJson(array);
    }

    @ResponseBody
    @RequestMapping("goods")
    public ResponseJson getGoodsByBillNumber(SearchVo searchVo) {
//        List<String> billNumbers = searchVo.getBillNumbers();
//        String companyName = searchVo.getCompanyName();
//        List<CAcceptanceGoodsInfo> goodsInfos = new ArrayList<>();
//        for (String billNumber : billNumbers) {
//            List<SellBillItem> list = steelWaiterService.getGoodsByBillNumber(billNumber,companyName);
//            SellBill sellBill = steelWaiterService.getSellBillByNumber(billNumber, companyName);
//            if(CollectionUtil.isNotEmpty(list)){
//                List<CAcceptanceGoodsInfo> collect = list.stream().map(r -> {
//                    CAcceptanceGoodsInfo acceptanceGoodsInfo = new CAcceptanceGoodsInfo(r);
//                    acceptanceGoodsInfo.setAcceptanceDate(sellBill.getReceiptDate());
//                    return acceptanceGoodsInfo;
//                }).collect(Collectors.toList());
//                goodsInfos.addAll(collect);
//            }
//        }
//        return new ResponseJson(goodsInfos);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);


        //用HttpEntity封装整个请求报文
        HttpEntity<SearchVo> request = new HttpEntity<>(searchVo, headers);

        String s = restTemplate.postForObject(steelWaiterGoods, searchVo, String.class);
        JSONArray array = JSONArray.parseArray(s);
        return new ResponseJson(array);

    }

    @ResponseBody
    @RequestMapping("file")
    public ResponseJson getFileByBillNumber(SearchVo searchVo) {
        List<String> billNumbers = searchVo.getBillNumbers();
        String companyName = searchVo.getCompanyName();
        List<FileStorageDO> fileStorages = new ArrayList<>();
        for (String billNumber : billNumbers) {
            List<FileStorageDO> fileByBillNumber = steelWaiterService.getFileByBillNumber(billNumber, companyName);
            fileStorages.addAll(fileByBillNumber);
        }
        return new ResponseJson(fileStorages);
    }
}

