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.DemEnforceReportAttach;
import com.usky.fire.domain.DemEnforceReportComplaint;
import com.usky.fire.mapper.DemEnforceReportAttachMapper;
import com.usky.fire.mapper.DemEnforceReportComplaintMapper;
import com.usky.fire.service.DemEnforceReportAttachService;
import com.usky.fire.service.DemEnforceReportComplaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 监督检查举报投诉信息附表 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-09-23
 */
@Service
public class DemEnforceReportAttachServiceImpl extends AbstractCrudService<DemEnforceReportAttachMapper, DemEnforceReportAttach> implements DemEnforceReportAttachService {

    @Autowired
    private DemEnforceReportComplaintMapper demEnforceReportComplaintMapper;

    @Override
    public int enforceReportAttachCountOne(String workOrderStatusName, String satisfiedType) {
        Integer integer = baseMapper.selectCount1(workOrderStatusName, satisfiedType);
        return integer;
    }

    @Override
    public int enforceReportAttachCount(String workOrderStatusName, String satisfiedType) {
        int count = 0;
        LambdaQueryWrapper<DemEnforceReportComplaint> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemEnforceReportComplaint::getReportId)
                .eq(DemEnforceReportComplaint::getEnable, 0);
        if (StringUtils.isNotBlank(satisfiedType)){
            queryWrapper.eq( DemEnforceReportComplaint::getSatisfiedType, Integer.parseInt(satisfiedType));
        }
        List<DemEnforceReportComplaint> list = demEnforceReportComplaintMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            List<String> reportIdList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                reportIdList.add(list.get(i).getReportId());
            }
            LambdaQueryWrapper<DemEnforceReportAttach> queryWrapper1 = Wrappers.lambdaQuery();
            if (StringUtils.isNotBlank(workOrderStatusName)) {
                queryWrapper1.eq(DemEnforceReportAttach::getWorkOrderStatusName, workOrderStatusName);
            } else {
                queryWrapper1.ne(DemEnforceReportAttach::getWorkOrderStatusName, "已撤销");
            }
            queryWrapper1.in(DemEnforceReportAttach::getReportId, reportIdList);
            count = this.count(queryWrapper1);
        }
        return count;
    }

}
