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.StringUtils;
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.common.core.util.IpUtils;
import com.usky.common.core.util.ServletUtils;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.DemEnforceInspectRecord;
import com.usky.fire.mapper.DemEnforceInspectRecordMapper;
import com.usky.fire.service.DemEnforceInspectRecordService;
import com.usky.fire.service.vo.DemEnforceInspectRecordVO;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 消防监督检查记录 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-09-22
 */
@Service
public class DemEnforceInspectRecordServiceImpl extends AbstractCrudService<DemEnforceInspectRecordMapper, DemEnforceInspectRecord> implements DemEnforceInspectRecordService {

    @Override
    public CommonPage<Object> enforceInspectRecordList(String projectCode, String companyName, Integer pageNum, Integer pageSize) {
        List<Object> list = new ArrayList<>();
        IPage<DemEnforceInspectRecord> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DemEnforceInspectRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemEnforceInspectRecord::getId, DemEnforceInspectRecord::getProjectCode, DemEnforceInspectRecord::getCompanyName,
                DemEnforceInspectRecord::getCompanyAddress, DemEnforceInspectRecord::getInspectDescript, DemEnforceInspectRecord::getInspectName,
                DemEnforceInspectRecord::getFireDutyName, DemEnforceInspectRecord::getFireDutyPhone, DemEnforceInspectRecord::getFireManageName,
                DemEnforceInspectRecord::getFireManagePhone, DemEnforceInspectRecord::getCompanyNatureName, DemEnforceInspectRecord::getEnforceInspectPerson,
                DemEnforceInspectRecord::getSafeManageContent, DemEnforceInspectRecord::getFireProtectContent, DemEnforceInspectRecord::getSafeEvacuatContent,
                DemEnforceInspectRecord::getBuildArea, DemEnforceInspectRecord::getBuildFloor, DemEnforceInspectRecord::getBuildHigh,
                DemEnforceInspectRecord::getIllegalAct, DemEnforceInspectRecord::getHandlingSituation, DemEnforceInspectRecord::getInspectQualified)
                .eq(DemEnforceInspectRecord::getEnable, 0)
                .like(StringUtils.isNotBlank(projectCode), DemEnforceInspectRecord::getProjectCode, projectCode)
                .like(StringUtils.isNotBlank(companyName), DemEnforceInspectRecord::getCompanyName, companyName)
                .orderByDesc(DemEnforceInspectRecord::getId);
        page = this.page(page, queryWrapper);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            for (int i = 0; i < page.getRecords().size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", page.getRecords().get(i).getId());
                map.put("projectCode", page.getRecords().get(i).getProjectCode());
                map.put("companyName", page.getRecords().get(i).getCompanyName());
                map.put("companyAddress", page.getRecords().get(i).getCompanyAddress());
                map.put("inspectDescript", page.getRecords().get(i).getInspectDescript());
                map.put("inspectName", page.getRecords().get(i).getInspectName());
                map.put("fireDutyName", page.getRecords().get(i).getFireDutyName());
                map.put("fireDutyPhone", page.getRecords().get(i).getFireDutyPhone());
                map.put("fireManageName", page.getRecords().get(i).getFireManageName());
                map.put("fireManagePhone", page.getRecords().get(i).getFireManagePhone());
                map.put("companyNatureName", page.getRecords().get(i).getCompanyNatureName());
                map.put("enforceInspectPerson", page.getRecords().get(i).getEnforceInspectPerson());
                map.put("safeManageContent", page.getRecords().get(i).getSafeManageContent());
                map.put("fireProtectContent", page.getRecords().get(i).getFireProtectContent());
                map.put("safeEvacuatContent", page.getRecords().get(i).getSafeEvacuatContent());
                map.put("buildArea", page.getRecords().get(i).getBuildArea());
                map.put("buildFloor", page.getRecords().get(i).getBuildFloor());
                map.put("buildHigh", page.getRecords().get(i).getBuildHigh());
                map.put("illegalAct", page.getRecords().get(i).getIllegalAct());
                map.put("handlingSituation", page.getRecords().get(i).getHandlingSituation());
                map.put("inspectQualified", page.getRecords().get(i).getInspectQualified());
                list.add(map);
            }
        }
        return new CommonPage<>(list, page.getTotal(), pageSize, pageNum);
    }

    @Override
    public List<DemEnforceInspectRecordVO> enforceInspectRecordListExport(String projectCode, String companyName){
        List<DemEnforceInspectRecordVO> list = new ArrayList<>();
        LambdaQueryWrapper<DemEnforceInspectRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemEnforceInspectRecord::getId, DemEnforceInspectRecord::getProjectCode, DemEnforceInspectRecord::getCompanyName,
                DemEnforceInspectRecord::getCompanyAddress, DemEnforceInspectRecord::getInspectDescript, DemEnforceInspectRecord::getInspectName,
                DemEnforceInspectRecord::getFireDutyName, DemEnforceInspectRecord::getFireDutyPhone, DemEnforceInspectRecord::getFireManageName,
                DemEnforceInspectRecord::getFireManagePhone, DemEnforceInspectRecord::getCompanyNatureName, DemEnforceInspectRecord::getEnforceInspectPerson,
                DemEnforceInspectRecord::getSafeManageContent, DemEnforceInspectRecord::getFireProtectContent, DemEnforceInspectRecord::getSafeEvacuatContent,
                DemEnforceInspectRecord::getBuildArea, DemEnforceInspectRecord::getBuildFloor, DemEnforceInspectRecord::getBuildHigh,
                DemEnforceInspectRecord::getIllegalAct, DemEnforceInspectRecord::getHandlingSituation, DemEnforceInspectRecord::getInspectQualified)
                .eq(DemEnforceInspectRecord::getEnable, 0)
                .like(StringUtils.isNotBlank(projectCode), DemEnforceInspectRecord::getProjectCode, projectCode)
                .like(StringUtils.isNotBlank(companyName), DemEnforceInspectRecord::getCompanyName, companyName)
                .orderByDesc(DemEnforceInspectRecord::getId).last("limit 1000");
        List<DemEnforceInspectRecord> list1 = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list1)) {
            for (int i = 0; i < list1.size(); i++) {
                DemEnforceInspectRecordVO demEnforceInspectRecordVO = new DemEnforceInspectRecordVO();
                demEnforceInspectRecordVO.setProjectCode(list1.get(i).getProjectCode());
                demEnforceInspectRecordVO.setCompanyName(list1.get(i).getCompanyName());
                demEnforceInspectRecordVO.setCompanyAddress(list1.get(i).getCompanyAddress());
                demEnforceInspectRecordVO.setInspectName(list1.get(i).getInspectName());
                demEnforceInspectRecordVO.setInspectDescript(list1.get(i).getInspectDescript());
                demEnforceInspectRecordVO.setFireDutyName(list1.get(i).getFireDutyName());
                demEnforceInspectRecordVO.setFireDutyPhone(list1.get(i).getFireDutyPhone());
                demEnforceInspectRecordVO.setFireManageName(list1.get(i).getFireManageName());
                demEnforceInspectRecordVO.setFireManagePhone(list1.get(i).getFireManagePhone());
                demEnforceInspectRecordVO.setCompanyNatureName(list1.get(i).getCompanyNatureName());
                demEnforceInspectRecordVO.setEnforceInspectPerson(list1.get(i).getEnforceInspectPerson());
                demEnforceInspectRecordVO.setBuildArea(list1.get(i).getBuildArea());
                demEnforceInspectRecordVO.setIllegalAct(list1.get(i).getIllegalAct());
                demEnforceInspectRecordVO.setInspectQualified(list1.get(i).getInspectQualified());
                demEnforceInspectRecordVO.setBuildFloor(list1.get(i).getBuildFloor());
                demEnforceInspectRecordVO.setBuildHigh(list1.get(i).getBuildHigh());
                demEnforceInspectRecordVO.setHandlingSituation(list1.get(i).getHandlingSituation());
                list.add(demEnforceInspectRecordVO);
            }
        }

        return list;
    }

    @Override
    public void updateEnforceInspectRecord(DemEnforceInspectRecord demEnforceInspectRecord) {
        String ipAddr = IpUtils.getIpAddr(ServletUtils.getRequest());
        demEnforceInspectRecord.setUpdateIp(ipAddr);
        demEnforceInspectRecord.setUpdateName(SecurityUtils.getLoginUser().getSysPerson().getFullName());
        this.updateById(demEnforceInspectRecord);
    }

    @Override
    public void delEnforceInspectRecord(Integer id) {
        DemEnforceInspectRecord demEnforceInspectRecord = new DemEnforceInspectRecord();
        String ipAddr = IpUtils.getIpAddr(ServletUtils.getRequest());
        demEnforceInspectRecord.setUpdateName(SecurityUtils.getLoginUser().getSysPerson().getFullName());
        demEnforceInspectRecord.setUpdateIp(ipAddr);
        demEnforceInspectRecord.setId(id);
        demEnforceInspectRecord.setEnable(1);
        this.updateById(demEnforceInspectRecord);
    }

}
