package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.DemBuildScore;
import com.usky.fire.mapper.DemBuildScoreMapper;
import com.usky.fire.service.DemBuildScoreService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 建筑评分 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-09-06
 */
@Service
public class DemBuildScoreServiceImpl extends AbstractCrudService<DemBuildScoreMapper, DemBuildScore> implements DemBuildScoreService {

    @Override
    @Transactional
    public void buildScoreUpdate(List<DemBuildScore> demBuildScoreList) {
        if (CollectionUtils.isEmpty(demBuildScoreList)) {
            throw new BusinessException("参数不可为空");
        }
        for (int i = 0; i < demBuildScoreList.size(); i++) {
            demBuildScoreList.get(i).setUpdateTime(LocalDateTime.now());
            demBuildScoreList.get(i).setUpdatePerson(SecurityUtils.getUsername());
            this.updateById(demBuildScoreList.get(i));
        }
    }

    @Override
    public List<Object> buildScoreList() {
        List<Object> list = new ArrayList<>();
        LambdaQueryWrapper<DemBuildScore> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DemBuildScore::getEnable, 1);
        List<DemBuildScore> list1 = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list1)) {
            for (int i = 0; i < list1.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", list1.get(i).getId());
                map.put("weightName", list1.get(i).getWeightName());
                map.put("riskId", list1.get(i).getRiskId());
                map.put("riskMax", list1.get(i).getRiskMax());
                map.put("buildScore", list1.get(i).getBuildScore());
                map.put("weightProportion", list1.get(i).getWeightProportion());
                list.add(map);
            }
        }
        return list;
    }

    @Override
    public List<DemBuildScore> buildScoreListOne(){
        LambdaQueryWrapper<DemBuildScore> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DemBuildScore::getEnable, 1);
        List<DemBuildScore> list1 = this.list(queryWrapper);
        return list1;
    }

}
