PriceServiceImpl.java 3.1 KB
package com.lhcredit.project.business.price.service;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lhcredit.framework.web.domain.AjaxResult;
import com.lhcredit.project.business.price.domain.Price;
import com.lhcredit.project.business.price.mapper.PriceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.*;

@Service
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public class PriceServiceImpl implements PriceService{
    @Value("${crm.url}")
    private String apiUrl;

    @Value("${crm.token}")
    private String token;

    @Autowired
    private PriceMapper priceMapper;

    /**
     * 添加用户报告价格
     * @param contractId 合同号
     */
    @Override
    public AjaxResult addPrice(String contractId) {
        String priceUrl = apiUrl+"/contract/client/web/selectBusinessTypeByContractId?id="+contractId;
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        List<Price> priceByContractId = priceMapper.findPriceByContractId(contractId);
        if (!ObjectUtils.isEmpty(priceByContractId)){
            return AjaxResult.error("已经存在价格数据");
        }
        String result = HttpRequest.get(priceUrl).addHeaders(map).execute().body();
        if(!"".equals(result)||null!=result){
            JSONObject obj= (JSONObject) JSONObject.parse(result);
            if(obj.getIntValue("code")==200){
                JSONArray array = obj.getJSONArray("data");
                if(array.size()>0) {
                    array.forEach(a->{
                        JSONObject obj1 = (JSONObject) a;
                        //先查一遍是否存在
                        List<Price> price = new ArrayList<>();
                        if (obj1.get("isCheck").toString().equals("Y")){
                            Price price1 = new Price();
                            price1.setContractId(obj1.get("contractId").toString());
                            price1.setTypePrice(obj1.get("typePrice").toString());
                            price1.setDeliveryDays(obj1.get("deliveryDays").toString());
                            price1.setDataStatus(obj1.get("dataStatus").toString());
                            price1.setClientType(obj1.get("clientType").toString());
                            price.add(price1);
                        }
                        priceMapper.addPrice(price);
                    });
                }
            }
        }
        return AjaxResult.success("添加价格成功");
    }
    /**
     * 根据合同单号查找 价格
     * @param contractId
     * @return
     */
    @Override
    public List<Price> findPriceByContractId(String contractId) {
        return priceMapper.findPriceByContractId(contractId);
    }
}