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.exception.BusinessException;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.domain.DemEnforceReportAttach;
import com.usky.fire.domain.DemEnforceReportComplaint;
import com.usky.fire.domain.DemEnforceReportLabel;
import com.usky.fire.domain.DemEnforceReportLabelAttribute;
import com.usky.fire.mapper.DemEnforceReportComplaintMapper;
import com.usky.fire.service.DemEnforceReportAttachService;
import com.usky.fire.service.DemEnforceReportComplaintService;
import com.usky.fire.service.DemEnforceReportLabelAttributeService;
import com.usky.fire.service.DemEnforceReportLabelService;
import com.usky.fire.service.util.OnlineMethod;
import com.usky.fire.service.vo.ParameterVo;
import com.usky.fire.service.vo.DemEnforceReportComplaintVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 监督检查举报投诉信息 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-09-23
 */
@Service
public class DemEnforceReportComplaintServiceImpl extends AbstractCrudService<DemEnforceReportComplaintMapper, DemEnforceReportComplaint> implements DemEnforceReportComplaintService {

    @Autowired
    private DemEnforceReportAttachService demEnforceReportAttachService;

    @Autowired
    private DemEnforceReportLabelService demEnforceReportLabelService;

    @Autowired
    private DemEnforceReportLabelAttributeService demEnforceReportLabelAttributeService;

    @Autowired
    private DemEnforceReportLabelServiceImpl demEnforceReportLabelServiceImpl;

    @Override
    public CommonPage<Object> enforceReportComplaintList(String serialNumber, String reportPhone, Integer id, Integer pageNum, Integer pageSize) {
        List<Object> list = new ArrayList<>();
        IPage<DemEnforceReportComplaint> page = new Page<>(pageNum, pageSize);
        IPage<DemEnforceReportAttach> page1 = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DemEnforceReportComplaint> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemEnforceReportComplaint::getId, DemEnforceReportComplaint::getReportId, DemEnforceReportComplaint::getSerialNumber,
                DemEnforceReportComplaint::getReportPhone, DemEnforceReportComplaint::getReportPerson, DemEnforceReportComplaint::getWorkOrderStatus,
                DemEnforceReportComplaint::getReportMethod, DemEnforceReportComplaint::getReportRecord, DemEnforceReportComplaint::getReportSource,
                DemEnforceReportComplaint::getReportAddress, DemEnforceReportComplaint::getReportContent, DemEnforceReportComplaint::getReportTime,
                DemEnforceReportComplaint::getAcceptTime, DemEnforceReportComplaint::getWorkOrderType, DemEnforceReportComplaint::getGovernCompany,
                DemEnforceReportComplaint::getGovernArea, DemEnforceReportComplaint::getFactFind, DemEnforceReportComplaint::getTypeId1,
                DemEnforceReportComplaint::getTypeId2, DemEnforceReportComplaint::getTypeId3, DemEnforceReportComplaint::getTypeId4,
                DemEnforceReportComplaint::getTypeId5)
                .eq(DemEnforceReportComplaint::getEnable, 0)
                .like(StringUtils.isNotBlank(serialNumber), DemEnforceReportComplaint::getSerialNumber, serialNumber)
                .like(StringUtils.isNotBlank(reportPhone), DemEnforceReportComplaint::getReportPhone, reportPhone)
                .eq(id != 0 & id != null, DemEnforceReportComplaint::getId, id)
                .orderByDesc(DemEnforceReportComplaint::getId);
        page = this.page(page, queryWrapper);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            List<String> reportIdList = new ArrayList<>();
            for (int i = 0; i < page.getRecords().size(); i++) {
                reportIdList.add(page.getRecords().get(i).getReportId());
            }
            LambdaQueryWrapper<DemEnforceReportAttach> queryWrapper1 = Wrappers.lambdaQuery();
            queryWrapper1.select(DemEnforceReportAttach::getId, DemEnforceReportAttach::getReportId,
                    DemEnforceReportAttach::getCompleteTime, DemEnforceReportAttach::getFollowUpStatus)
                    .in(DemEnforceReportAttach::getReportId, reportIdList);
            page1 = demEnforceReportAttachService.page(page1, queryWrapper1);
            LambdaQueryWrapper<DemEnforceReportLabel> queryWrapper2 = Wrappers.lambdaQuery();
            queryWrapper2.select(DemEnforceReportLabel::getId, DemEnforceReportLabel::getLabelName);
            List<DemEnforceReportLabel> list1 = demEnforceReportLabelService.list(queryWrapper2);
            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("serialNumber", page.getRecords().get(i).getSerialNumber());
                map.put("reportPhone", page.getRecords().get(i).getReportPhone());
                map.put("reportPerson", page.getRecords().get(i).getReportPerson());
                map.put("workOrderStatus", page.getRecords().get(i).getWorkOrderStatus());
                map.put("reportMethod", page.getRecords().get(i).getReportMethod());
                map.put("reportRecord", page.getRecords().get(i).getReportRecord());
                map.put("reportSource", page.getRecords().get(i).getReportSource());
                map.put("reportAddress", page.getRecords().get(i).getReportAddress());
                map.put("reportContent", page.getRecords().get(i).getReportContent());
                map.put("reportTime", page.getRecords().get(i).getReportTime());
                map.put("acceptTime", page.getRecords().get(i).getAcceptTime());
                map.put("workOrderType", page.getRecords().get(i).getWorkOrderType());
                map.put("governCompany", page.getRecords().get(i).getGovernCompany());
                map.put("governArea", page.getRecords().get(i).getGovernArea());
                map.put("factFind", page.getRecords().get(i).getFactFind());
                map.put("satisfiedType", page.getRecords().get(i).getSatisfiedType());
                map.put("typeId1", page.getRecords().get(i).getTypeId1());
                map.put("typeId2", page.getRecords().get(i).getTypeId2());
                map.put("typeId3", page.getRecords().get(i).getTypeId3());
                map.put("typeId4", page.getRecords().get(i).getTypeId4());
                map.put("typeId5", page.getRecords().get(i).getTypeId5());
                map.put("typeName1", null);
                map.put("typeName2", null);
                map.put("typeName3", null);
                map.put("typeName4", null);
                map.put("typeName5", null);
                map.put("id1", null);
                map.put("completeTime", null);
                map.put("followUpStatus", null);
                map.put("labelName", null);
                for (int j = 0; j < page1.getRecords().size(); j++) {
                    if (page.getRecords().get(i).getReportId().equals(page1.getRecords().get(j).getReportId())) {
                        map.put("id1", page1.getRecords().get(j).getId());
                        map.put("completeTime", page1.getRecords().get(j).getCompleteTime());
                        map.put("followUpStatus", page1.getRecords().get(j).getFollowUpStatus());

                    }
                }
                for (int j = 0; j < list1.size(); j++) {
                    if (page.getRecords().get(i).getTypeId1() != null && (int) page.getRecords().get(i).getTypeId1() == (int) list1.get(j).getId()) {
                        map.put("typeName1", list1.get(j).getLabelName());
                    }
                    if (page.getRecords().get(i).getTypeId2() != null && (int) page.getRecords().get(i).getTypeId2() == (int) list1.get(j).getId()) {
                        map.put("typeName2", list1.get(j).getLabelName());
                    }
                    if (page.getRecords().get(i).getTypeId3() != null && (int) page.getRecords().get(i).getTypeId3() == (int) list1.get(j).getId()) {
                        map.put("typeName3", list1.get(j).getLabelName());
                    }
                    if (page.getRecords().get(i).getTypeId4() != null && (int) page.getRecords().get(i).getTypeId4() == (int) list1.get(j).getId()) {
                        map.put("typeName4", list1.get(j).getLabelName());
                    }
                    if (page.getRecords().get(i).getTypeId5() != null && (int) page.getRecords().get(i).getTypeId5() == (int) list1.get(j).getId()) {
                        map.put("typeName5", list1.get(j).getLabelName());
                    }
                }
                list.add(map);
            }
        }
        return new CommonPage<>(list, page.getTotal(), pageSize, pageNum);
    }

    @Override
    public List<DemEnforceReportComplaintVO> enforceReportComplaintListExport(String serialNumber, String reportPhone){
        List<DemEnforceReportComplaintVO> list = new ArrayList<>();
        LambdaQueryWrapper<DemEnforceReportComplaint> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemEnforceReportComplaint::getReportId, DemEnforceReportComplaint::getSerialNumber, DemEnforceReportComplaint::getReportPerson,
                DemEnforceReportComplaint::getReportPhone, DemEnforceReportComplaint::getWorkOrderStatus, DemEnforceReportComplaint::getReportMethod,
                DemEnforceReportComplaint::getReportAddress, DemEnforceReportComplaint::getReportContent,DemEnforceReportComplaint::getReportTime,
                DemEnforceReportComplaint::getAcceptTime, DemEnforceReportComplaint::getWorkOrderType, DemEnforceReportComplaint::getGovernCompany,
                DemEnforceReportComplaint::getGovernArea, DemEnforceReportComplaint::getFactFind, DemEnforceReportComplaint::getReportRecord,
                DemEnforceReportComplaint::getReportSource, DemEnforceReportComplaint::getSatisfiedType)
                .eq(DemEnforceReportComplaint::getEnable, 0)
                .like(StringUtils.isNotBlank(serialNumber), DemEnforceReportComplaint::getSerialNumber, serialNumber)
                .like(StringUtils.isNotBlank(reportPhone), DemEnforceReportComplaint::getReportPhone, reportPhone)
                .orderByDesc(DemEnforceReportComplaint::getId);
        List<DemEnforceReportComplaint> list1 = this.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(list1)){
            List<String> reportIdList = new ArrayList<>();
            for (int i = 0; i < list1.size(); i++) {
                reportIdList.add(list1.get(i).getReportId());
            }
            List<DemEnforceReportAttach> list2 = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(reportIdList)){
                LambdaQueryWrapper<DemEnforceReportAttach> queryWrapper1 = Wrappers.lambdaQuery();
                queryWrapper1.select(DemEnforceReportAttach::getId, DemEnforceReportAttach::getReportId,
                        DemEnforceReportAttach::getCompleteTime, DemEnforceReportAttach::getFollowUpStatus)
                        .in(DemEnforceReportAttach::getReportId, reportIdList);
                list2 = demEnforceReportAttachService.list(queryWrapper1);
            }

            for(int i=0;i<list1.size();i++){
                DemEnforceReportComplaintVO demEnforceReportComplaintVO = new DemEnforceReportComplaintVO();
                demEnforceReportComplaintVO.setSerialNumber(list1.get(i).getSerialNumber());
                demEnforceReportComplaintVO.setReportPerson(list1.get(i).getReportPerson());
                demEnforceReportComplaintVO.setReportPhone(list1.get(i).getReportPhone());
                demEnforceReportComplaintVO.setWorkOrderStatus(list1.get(i).getWorkOrderStatus());
                demEnforceReportComplaintVO.setReportMethod(list1.get(i).getReportMethod());
                demEnforceReportComplaintVO.setReportAddress(list1.get(i).getReportAddress());
                demEnforceReportComplaintVO.setReportContent(list1.get(i).getReportContent());
                demEnforceReportComplaintVO.setReportTime((list1.get(i).getReportTime()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                demEnforceReportComplaintVO.setAcceptTime((list1.get(i).getAcceptTime()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                demEnforceReportComplaintVO.setWorkOrderType(list1.get(i).getWorkOrderType());
                demEnforceReportComplaintVO.setGovernCompany(list1.get(i).getGovernCompany());
                demEnforceReportComplaintVO.setGovernArea(list1.get(i).getGovernArea());
                String factFindName = null;
                if (StringUtils.isNotBlank(list1.get(i).getFactFind())) {
                    if (list1.get(i).getFactFind().equals("1")) {
                        factFindName = "属实";
                    } else if (list1.get(i).getFactFind().equals("2")) {
                        factFindName = "部分属实";
                    } else if (list1.get(i).getFactFind().equals("3")) {
                        factFindName = "不属实";
                    }
                }
                demEnforceReportComplaintVO.setFactFind(factFindName);
                for(int j=0;j<list2.size();j++){
                    if(list1.get(i).getReportId().equals(list2.get(j).getReportId())){
                        if((list2.get(i).getCompleteTime()) != null){
                            demEnforceReportComplaintVO.setCompleteTime((list2.get(i).getCompleteTime()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                        }else{
                            demEnforceReportComplaintVO.setCompleteTime(null);
                        }

                        String followUpStatusName = null;
                        if (list2.get(i).getFollowUpStatus() == 0) {
                            followUpStatusName = "未回访";
                        } else if (list2.get(i).getFollowUpStatus() == 1) {
                            followUpStatusName = "已回访";
                        }
                        demEnforceReportComplaintVO.setFollowUpStatus(followUpStatusName);
                    }
                }

                demEnforceReportComplaintVO.setReportRecord(list1.get(i).getReportRecord());
                demEnforceReportComplaintVO.setReportSource(list1.get(i).getReportSource());
                demEnforceReportComplaintVO.setSatisfiedType(list1.get(i).getSatisfiedType());

                list.add(demEnforceReportComplaintVO);
            }
        }

        return list;
    }

    @Transactional
    @Override
    public void addLabel(DemEnforceReportLabel demEnforceReportLabel) {
        if (demEnforceReportLabel.getId() == 0) {
            LambdaQueryWrapper<DemEnforceReportLabelAttribute> queryWrapper = Wrappers.lambdaQuery();
            List<DemEnforceReportLabelAttribute> list = demEnforceReportLabelAttributeService.list(queryWrapper);
            String typeAttribute = "typeName1";
            if (list.size() > 1) {
                List<String> typeIdList1 = new ArrayList<>();
                typeIdList1.add("typeName1");
                typeIdList1.add("typeName2");
                typeIdList1.add("typeName3");
                typeIdList1.add("typeName4");
                typeIdList1.add("typeName5");
                List<String> typeIdList2 = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    typeIdList2.add(list.get(i).getAttribute());
                }
                List<String> typeIdList = OnlineMethod.subList2(typeIdList1, typeIdList2);
                typeAttribute = typeIdList.get(0);
            }
            demEnforceReportLabelService.addEnforceReportLabel(demEnforceReportLabel);
            DemEnforceReportLabelAttribute demEnforceReportLabelAttribute = new DemEnforceReportLabelAttribute();
            demEnforceReportLabelAttribute.setLabelId(demEnforceReportLabel.getId());
            demEnforceReportLabelAttribute.setAttribute(typeAttribute);
            demEnforceReportLabelAttributeService.save(demEnforceReportLabelAttribute);
        } else {
            demEnforceReportLabelService.updateEnforceReportLabel(demEnforceReportLabel);
        }
        int fid = demEnforceReportLabel.getId();
        List<DemEnforceReportLabel> demEnforceReportLabel1 = demEnforceReportLabel.getSubset();
        if (CollectionUtils.isNotEmpty(demEnforceReportLabel1)) {
            List<DemEnforceReportLabel> demEnforceReportLabel2 = new ArrayList<>();
            List<DemEnforceReportLabel> demEnforceReportLabel3 = new ArrayList<>();
            List<String> idList1 = new ArrayList<>();
            List<String> idList2 = new ArrayList<>();
            for (int i = 0; i < demEnforceReportLabel1.size(); i++) {
                if (demEnforceReportLabel1.get(i).getId() == 0) {
                    demEnforceReportLabel2.add(demEnforceReportLabel1.get(i));
                } else {
                    demEnforceReportLabel3.add(demEnforceReportLabel1.get(i));
                    idList1.add(demEnforceReportLabel1.get(i).getId() + "");
                }
            }
            List<DemEnforceReportLabel> demLabels = demEnforceReportLabelService.enforceReportLabelSelect(fid);
            if (CollectionUtils.isNotEmpty(demLabels)) {
                for (int i = 0; i < demLabels.size(); i++) {
                    idList2.add(demLabels.get(i).getId() + "");
                }
            }
            //删除
            List<String> idlist = OnlineMethod.subList2(idList2, idList1);
            if (CollectionUtils.isNotEmpty(idlist)) {
                for (int i = 0; i < idlist.size(); i++) {
                    demEnforceReportLabelService.delEnforceReportLabel(Integer.parseInt(idlist.get(i)));
                }
            }

            //修改
            if (CollectionUtils.isNotEmpty(demEnforceReportLabel3)) {
                for (int i = 0; i < demEnforceReportLabel3.size(); i++) {
                    demEnforceReportLabelService.updateEnforceReportLabel(demEnforceReportLabel3.get(i));
                }
            }

            //新增
            if (CollectionUtils.isNotEmpty(demEnforceReportLabel2)) {
                for (int i = 0; i < demEnforceReportLabel2.size(); i++) {
                    demEnforceReportLabel2.get(i).setFid(fid);
                    demEnforceReportLabelService.addEnforceReportLabel(demEnforceReportLabel2.get(i));
                }
            }
        }
    }

    @Transactional
    @Override
    public void bindLabel(ParameterVo parameterVo) {
        LambdaQueryWrapper<DemEnforceReportLabelAttribute> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DemEnforceReportLabelAttribute::getLabelId, parameterVo.getLabelId());
        List<DemEnforceReportLabelAttribute> list = demEnforceReportLabelAttributeService.list(queryWrapper);
        for (int i = 0; i < parameterVo.getIdList().size(); i++) {
            DemEnforceReportComplaint demEnforceReportComplaint = new DemEnforceReportComplaint();
            demEnforceReportComplaint.setId(parameterVo.getIdList().get(i));
            switch (list.get(0).getAttribute()) {
                case "typeName1":
                    demEnforceReportComplaint.setTypeId1(parameterVo.getClassifyId());
                    break;
                case "typeName2":
                    demEnforceReportComplaint.setTypeId2(parameterVo.getClassifyId());
                    break;
                case "typeName3":
                    demEnforceReportComplaint.setTypeId3(parameterVo.getClassifyId());
                    break;
                case "typeName4":
                    demEnforceReportComplaint.setTypeId4(parameterVo.getClassifyId());
                    break;
                case "typeName5":
                    demEnforceReportComplaint.setTypeId5(parameterVo.getClassifyId());
                    break;
            }
            this.updateById(demEnforceReportComplaint);
        }
    }

    @Transactional
    @Override
    public void updateEnforceReportComplaint(DemEnforceReportComplaint demEnforceReportComplaint) {
        this.updateById(demEnforceReportComplaint);
        DemEnforceReportAttach demEnforceReportAttach = new DemEnforceReportAttach();
        demEnforceReportAttach.setId(demEnforceReportComplaint.getId1());
        demEnforceReportAttach.setCompleteTime(demEnforceReportComplaint.getCompleteTime());
        demEnforceReportAttach.setFollowUpStatus(demEnforceReportComplaint.getFollowUpStatus());
        demEnforceReportAttachService.updateById(demEnforceReportAttach);
    }

    @Override
    public void delEnforceReportComplaint(Integer id) {
        DemEnforceReportComplaint demEnforceReportComplaint = new DemEnforceReportComplaint();
        demEnforceReportComplaint.setId(id);
        demEnforceReportComplaint.setEnable(1);
        this.updateById(demEnforceReportComplaint);
    }

    @Override
    public void abnormalData(DemEnforceReportLabel demEnforceReportLabel) {
        List<Integer> typeIdList = new ArrayList<>();
        if (demEnforceReportLabel.getId() != null && demEnforceReportLabel.getId() != 0) {
            LambdaQueryWrapper<DemEnforceReportLabel> queryWrapper1 = Wrappers.lambdaQuery();
            queryWrapper1.eq(DemEnforceReportLabel::getFid, demEnforceReportLabel.getId());
            List<DemEnforceReportLabel> list1 = demEnforceReportLabelService.list(queryWrapper1);
            for (int i = 0; i < list1.size(); i++) {
                typeIdList.add(list1.get(i).getId());
            }
        } else {
            LambdaQueryWrapper<DemEnforceReportLabel> queryWrapper2 = Wrappers.lambdaQuery();
            queryWrapper2.eq(DemEnforceReportLabel::getLabelName, demEnforceReportLabel.getLabelName())
                    .eq(DemEnforceReportLabel::getFid, demEnforceReportLabel.getFid());
            List<DemEnforceReportLabel> list2 = demEnforceReportLabelService.list(queryWrapper2);
            for (int i = 0; i < list2.size(); i++) {
                typeIdList.add(list2.get(i).getId());
            }
        }
        if (CollectionUtils.isNotEmpty(typeIdList)) {
            LambdaQueryWrapper<DemEnforceReportComplaint> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.in(DemEnforceReportComplaint::getTypeId1, typeIdList)
                    .or()
                    .in(DemEnforceReportComplaint::getTypeId2, typeIdList)
                    .or()
                    .in(DemEnforceReportComplaint::getTypeId3, typeIdList)
                    .or()
                    .in(DemEnforceReportComplaint::getTypeId4, typeIdList)
                    .or()
                    .in(DemEnforceReportComplaint::getTypeId5, typeIdList);
            List<DemEnforceReportComplaint> list = this.list(queryWrapper);
            if (CollectionUtils.isNotEmpty(list)) {
                throw new BusinessException("标签/分类绑定中，不可删除");
            }
        }
    }

    @Override
    public void delEnforceReportLabel(Integer id) {
        DemEnforceReportLabel demEnforceReportLabel = new DemEnforceReportLabel();
        demEnforceReportLabel.setId(id);
        this.abnormalData(demEnforceReportLabel);
        demEnforceReportLabelServiceImpl.removeById(id);
        List<DemEnforceReportLabelAttribute> list = demEnforceReportLabelAttributeService.enforceReportLabelAttributeList(id);
        if (CollectionUtils.isNotEmpty(list)) {
            demEnforceReportLabelAttributeService.delEnforceReportLabelAttribute(list.get(0).getId());
        }
    }


}
