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.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.domain.DemBuildScoreRecord;
import com.usky.fire.mapper.DemBuildScoreRecordMapper;
import com.usky.fire.service.DemBuildScoreRecordService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 建筑评分历史记录表 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-09-08
 */
@Service
public class DemBuildScoreRecordServiceImpl extends AbstractCrudService<DemBuildScoreRecordMapper, DemBuildScoreRecord> implements DemBuildScoreRecordService {

    @Override
    public int demBuildScoreRecordCount(String scoreDate, Integer buildScore1, Integer buildScore2, String companyId) {
        LambdaQueryWrapper<DemBuildScoreRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotBlank(scoreDate), DemBuildScoreRecord::getScoreDate, scoreDate)
                .eq(StringUtils.isNotBlank(companyId), DemBuildScoreRecord::getCompanyId, companyId);
        if (buildScore2 != null && buildScore1 != null) {
            queryWrapper.between(buildScore2 != null && buildScore1 != null, DemBuildScoreRecord::getBuildScore, buildScore2, buildScore1);
        } else {
            queryWrapper.ge(buildScore2 != null, DemBuildScoreRecord::getBuildScore, buildScore2)
                    .le(buildScore1 != null, DemBuildScoreRecord::getBuildScore, buildScore1);
        }
        int count = this.count(queryWrapper);
        return count;
    }

    @Override
    public int demBuildScoreRecordCountOne(String scoreDate, Integer buildScore1, Integer buildScore2, List<String> companyIdList) {
        LambdaQueryWrapper<DemBuildScoreRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotBlank(scoreDate), DemBuildScoreRecord::getScoreDate, scoreDate)
                .in(CollectionUtils.isNotEmpty(companyIdList), DemBuildScoreRecord::getCompanyId, companyIdList);
        if (buildScore2 != null && buildScore1 != null) {
            queryWrapper.between(buildScore2 != null && buildScore1 != null, DemBuildScoreRecord::getBuildScore, (int)buildScore2, (int)buildScore1);
        } else {
            queryWrapper.ge(buildScore2 != null, DemBuildScoreRecord::getBuildScore, buildScore2)
                    .le(buildScore1 != null, DemBuildScoreRecord::getBuildScore, buildScore1);
        }
        int count = this.count(queryWrapper);
        return count;
    }

    @Override
    public List<DemBuildScoreRecord> demBuildScoreRecordList(String companyId, String scoreDate) {
        LambdaQueryWrapper<DemBuildScoreRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotBlank(companyId), DemBuildScoreRecord::getCompanyId, companyId)
                .eq(StringUtils.isNotBlank(scoreDate), DemBuildScoreRecord::getScoreDate, scoreDate)
                .orderByDesc(DemBuildScoreRecord::getId)
                .last(" limit 3");
        List<DemBuildScoreRecord> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public List<DemBuildScoreRecord> demBuildScoreRecordListOne(String scoreDate, String companyId) {
        LambdaQueryWrapper<DemBuildScoreRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotBlank(scoreDate), DemBuildScoreRecord::getScoreDate, scoreDate)
                .eq(StringUtils.isNotBlank(companyId), DemBuildScoreRecord::getCompanyId, companyId);
        List<DemBuildScoreRecord> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public void demBuildScoreRecordAdd(DemBuildScoreRecord demBuildScoreRecord) {
        demBuildScoreRecord.setCreateTime(LocalDateTime.now());
        this.save(demBuildScoreRecord);
    }

}
