package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.domain.BEnterpriseRiskAssessment;
import cn.cecep.talroad.domain.BEnterpriseRiskAssessmentLast;
import cn.cecep.talroad.domain.PcFactory;
import cn.cecep.talroad.mapper.BEnterpriseRiskAssessmentLastMapper;
import cn.cecep.talroad.mapper.BEnterpriseRiskAssessmentMapper;
import cn.cecep.talroad.mapper.PcFactoryMapper;
import cn.cecep.talroad.service.BEnterpriseRiskAssessmentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;


@Service
public class BEnterpriseRiskAssessmentServiceImpl implements BEnterpriseRiskAssessmentService {

    @Autowired
    private BEnterpriseRiskAssessmentMapper bEnterpriseRiskAssessmentMapper;

    @Autowired
    private BEnterpriseRiskAssessmentLastMapper bEnterpriseRiskAssessmentLastMapper;

    @Autowired
    private PcFactoryMapper pcFactoryMapper;

    @Override
    public Map<String,Object> getLastInfo(String factoryId) {
        Map<String,Object> map = new HashMap<>();
        List<BEnterpriseRiskAssessmentLast> list = bEnterpriseRiskAssessmentLastMapper.selectListByFactoryId(factoryId);
        map.put("list",list);
        map.put("score", Optional.ofNullable(pcFactoryMapper.selectOne(new LambdaQueryWrapper<PcFactory>().eq(PcFactory::getId,factoryId))).orElse(new PcFactory()).getScore());
        if (CollectionUtils.isEmpty(list) && map.get("score") == null) {
            map.put("score",100);
        }
        return map;
    }

    @Override
    public BEnterpriseRiskAssessment getInfo(String factoryId) {
        return bEnterpriseRiskAssessmentMapper.selectOne(new QueryWrapper<BEnterpriseRiskAssessment>().eq("factory_id", factoryId).orderByDesc("create_time").last("limit 1"));
    }
}
