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.common.security.utils.SecurityUtils;
import com.usky.fire.domain.DemFileLabel;
import com.usky.fire.domain.DemFireLabelAttribute;
import com.usky.fire.domain.DemLawSituation;
import com.usky.fire.mapper.DemFileLabelMapper;
import com.usky.fire.service.DemFileLabelService;
import com.usky.fire.service.DemFireLabelAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-09-14
 */
@Service
public class DemFileLabelServiceImpl extends AbstractCrudService<DemFileLabelMapper, DemFileLabel> implements DemFileLabelService {

    @Autowired
    private DemFireLabelAttributeService demFireLabelAttributeService;

    @Override
    public List<DemFileLabel> demFileLabelList(Integer fid, String labelName, List<Integer> idList) {
        LambdaQueryWrapper<DemFileLabel> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemFileLabel::getId, DemFileLabel::getFid, DemFileLabel::getLabelName)
                .eq(fid != null, DemFileLabel::getFid, fid)
                .in(CollectionUtils.isNotEmpty(idList), DemFileLabel::getId, idList)
                .like(StringUtils.isNotBlank(labelName), DemFileLabel::getLabelName, labelName)
                .orderByDesc(DemFileLabel::getId);
        List<DemFileLabel> list = this.list(queryWrapper);
        List<DemFireLabelAttribute> demFireLabelAttributes = demFireLabelAttributeService.freLabelAttributeList(null);
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < demFireLabelAttributes.size(); j++) {
                if (list.get(i).getId() == demFireLabelAttributes.get(j).getLabelId()) {
                    list.get(i).setAttribute(demFireLabelAttributes.get(j).getAttribute());
                }
            }
        }
        return list;
    }

    @Override
    public int demFileLabelCount(Integer fid, String labelName, Integer id) {
        LambdaQueryWrapper<DemFileLabel> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DemFileLabel::getFid, fid)
                .eq(DemFileLabel::getLabelName, labelName)
                .ne(id != null & id != 0, DemFileLabel::getId, id);
        int count = this.count(queryWrapper);
        return count;
    }

    @Override
    public void addDemFileLabel(DemFileLabel demFileLabel) {
        demFileLabel.setCreateTime(LocalDateTime.now());
        demFileLabel.setCreator(SecurityUtils.getUsername());
        this.save(demFileLabel);
    }

    @Override
    public void updateDemFileLabel(DemFileLabel demFileLabel) {
        demFileLabel.setUpdatePerson(SecurityUtils.getUsername());
        demFileLabel.setUpdateTime(LocalDateTime.now());
        this.updateById(demFileLabel);
    }

    @Override
    public void delDemFileLabel(Integer id) {
        this.removeById(id);
    }

}
