package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.fire.domain.BaseCompany;
import com.usky.fire.domain.DemFireHazardsCancellation;
import com.usky.fire.domain.DemFireHazardsNotice;
import com.usky.fire.mapper.DemFireHazardsNoticeMapper;
import com.usky.fire.service.BaseCompanyAttach1Service;
import com.usky.fire.service.DemFireHazardsCancellationService;
import com.usky.fire.service.DemFireHazardsNoticeService;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 重大火灾隐患整改通知书 服务实现类
 * </p>
 *
 * @author han
 * @since 2023-08-25
 */
@Service
public class DemFireHazardsNoticeServiceImpl extends AbstractCrudService<DemFireHazardsNoticeMapper, DemFireHazardsNotice> implements DemFireHazardsNoticeService {
    @Autowired
    private DemFireHazardsCancellationService demFireHazardsCancellationService;

    @Override
    public CommonPage<DemFireHazardsNotice> hazardsNoticeList(Integer listingLevel, Integer pageNum, Integer pageSize) {
        IPage<DemFireHazardsNotice> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DemFireHazardsNotice> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemFireHazardsNotice::getUserDesignId,DemFireHazardsNotice::getCompanyName,
                DemFireHazardsNotice::getCheckTime,DemFireHazardsNotice::getOrderCorrectTime,
                DemFireHazardsNotice::getContent,DemFireHazardsNotice::getBookNum,DemFireHazardsNotice::getListingLevel)
                .eq(listingLevel != null, DemFireHazardsNotice::getListingLevel,listingLevel)
                .eq(DemFireHazardsNotice::getDeleteFlag,0);
        page = this.page(page, queryWrapper);
        if (page.getTotal() > 0) {
            LambdaQueryWrapper<DemFireHazardsCancellation> queryWrapper1 = Wrappers.lambdaQuery();
            queryWrapper1.select(DemFireHazardsCancellation::getNoticeBookNum);
            List<DemFireHazardsCancellation> cancellationList = demFireHazardsCancellationService.list(queryWrapper1);
            if (CollectionUtils.isNotEmpty(cancellationList)){
                for (int j = 0; j < page.getRecords().size(); j++) {
                    page.getRecords().get(j).setNoticeState(0);
                    for (int k = 0; k < cancellationList.size(); k++) {
                        if (page.getRecords().get(j).getBookNum().equals(cancellationList.get(k).getNoticeBookNum())){
                            page.getRecords().get(j).setNoticeState(1);
                        }
                    }
                }
            }
        }
        return new CommonPage<>(page.getRecords(), page.getTotal(), pageSize, pageNum);
    }

    @Override
    public List<Map<String, Object>> hazardsNoticeStatistic() {
        List<Map<String, Object>> list = new ArrayList<>();
        LambdaQueryWrapper<DemFireHazardsNotice> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DemFireHazardsNotice::getDeleteFlag,0);
        int noticeNum = this.count(queryWrapper);
        LambdaQueryWrapper<DemFireHazardsCancellation> queryWrapper1 = Wrappers.lambdaQuery();
        int cancellationNum = demFireHazardsCancellationService.count(queryWrapper1);
        Map<String, Object> map = new HashMap<>();
        map.put("noticeNum", noticeNum);
        map.put("cancellationNum", cancellationNum);
        list.add(map);
        return list;
    }
}
