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.core.exception.BusinessException;
import com.usky.common.core.util.ServletUtils;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.DemEnforceReportLabel;
import com.usky.fire.domain.DemEnforceReportLabelAttribute;
import com.usky.fire.domain.DemFileLabel;
import com.usky.fire.domain.DemFireLabelAttribute;
import com.usky.fire.mapper.DemEnforceReportLabelMapper;
import com.usky.fire.service.DemEnforceReportLabelAttributeService;
import com.usky.fire.service.DemEnforceReportLabelService;
import com.usky.common.mybatis.core.AbstractCrudService;
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-26
 */
@Service
public class DemEnforceReportLabelServiceImpl extends AbstractCrudService<DemEnforceReportLabelMapper, DemEnforceReportLabel> implements DemEnforceReportLabelService {

    @Autowired
    private DemEnforceReportLabelAttributeService demEnforceReportLabelAttributeService;

    @Override
    public List<DemEnforceReportLabel> enforceReportLabelSelect(Integer fid){
        LambdaQueryWrapper<DemEnforceReportLabel> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemEnforceReportLabel::getId,DemEnforceReportLabel::getLabelName)
                .eq(fid!=null,DemEnforceReportLabel::getFid,fid)
                .orderByDesc(DemEnforceReportLabel::getId);
        List<DemEnforceReportLabel> list = this.list(queryWrapper);
        List<DemEnforceReportLabelAttribute> list1 = demEnforceReportLabelAttributeService.enforceReportLabelAttributeList(null);
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < list1.size(); j++) {
                if (list.get(i).getId() == list1.get(j).getLabelId()) {
                    list.get(i).setAttribute(list1.get(j).getAttribute());
                }
            }
        }
        return list;
    }

    @Override
    public void addEnforceReportLabel(DemEnforceReportLabel demEnforceReportLabel){
        LambdaQueryWrapper<DemEnforceReportLabel> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DemEnforceReportLabel::getFid,demEnforceReportLabel.getFid())
                .eq(DemEnforceReportLabel::getLabelName,demEnforceReportLabel.getLabelName());
        int count = this.count(queryWrapper);
        if (count>0){
            throw new BusinessException("标签/分类名称重复，请重新填写标签/分类名");
        }
        demEnforceReportLabel.setCreator(SecurityUtils.getUsername());
        demEnforceReportLabel.setCreateTime(LocalDateTime.now());
        this.save(demEnforceReportLabel);
    }

    @Override
    public void updateEnforceReportLabel(DemEnforceReportLabel demEnforceReportLabel){
        LambdaQueryWrapper<DemEnforceReportLabel> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.ne(DemEnforceReportLabel::getId,demEnforceReportLabel.getId())
                .eq(DemEnforceReportLabel::getLabelName,demEnforceReportLabel.getLabelName())
                .eq(DemEnforceReportLabel::getFid,demEnforceReportLabel.getFid());
        int count = this.count(queryWrapper);
        if (count>0){
            throw new BusinessException("标签/分类名称重复，请重新填写标签/分类名");
        }
        demEnforceReportLabel.setUpdatePerson(SecurityUtils.getUsername());
        demEnforceReportLabel.setUpdateTime(LocalDateTime.now());
        this.updateById(demEnforceReportLabel);
    }

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