package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.DemFireReform;
import com.usky.fire.domain.DemReformReview;
import com.usky.fire.mapper.DemFireReformMapper;
import com.usky.fire.service.DemFireReformService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.service.DemReformReviewService;
import com.usky.fire.service.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * <p>
 * 消防整改数据表 服务实现类
 * </p>
 *
 * @author ZYJ
 * @since 2023-02-24
 */
@Service
public class DemFireReformServiceImpl extends AbstractCrudService<DemFireReformMapper, DemFireReform> implements DemFireReformService {

    @Autowired
    private DemReformReviewService demReformReviewService;

    @Override
    public CommonPage<DemFireReformVO> fireReformList(String companyName, String reformId, Integer companyType, Integer pageNum, Integer pageSize){
        List<DemFireReformVO> list1 = baseMapper.fireReformList(companyName, reformId, companyType, null, null);
        int total = 0;
        if(list1.size() > 0){
            total = list1.size();
        }
        int current = 0;
        if(pageNum != null && pageSize > 0){
            current = (pageNum-1)*pageSize;
        }
        List<DemFireReformVO> list = baseMapper.fireReformList(companyName, reformId, companyType, current, pageSize);
        return new CommonPage<>(list, total, pageSize, pageNum);
    }

    @Override
    public CommonPage<MobileScheduleVO> mobileScheduleList(String companyId, String reformId, Integer reformStatus, Integer pageNum, Integer pageSize){
        List<MobileScheduleVO> list1 = baseMapper.mobileScheduleList(companyId, reformId, reformStatus, null, null);
        int total = 0;
        if(list1.size() > 0){
            total = list1.size();
        }
        int current = 0;
        if(pageNum > 0 && pageSize > 0){
            current = (pageNum - 1)*pageSize;
        }
        List<MobileScheduleVO> list = baseMapper.mobileScheduleList(companyId, reformId, reformStatus, current, pageSize);
        return new CommonPage<>(list, total, pageSize, pageNum);
    }

    @Override
    public List<Map<String, Object>> reformReview(String reformId){
        List<Map<String, Object>> list = new ArrayList<>();
        List<DemReformReviewVO> list1 = baseMapper.reformReview(reformId);
        if(CollectionUtils.isNotEmpty(list1)){
            for(int i=0;i<list1.size();i++){
                Map<String, Object> map = new HashMap<>();
                map.put("reformId", list1.get(i).getReFormId());
                map.put("reformPart",list1.get(i).getReformPart());
                map.put("sendTime",list1.get(i).getSendTime());
                map.put("reformTime",list1.get(i).getReformTime());
                map.put("reformPath1",list1.get(i).getReformPath1());
                map.put("reformPath2",list1.get(i).getReformPath2());
                map.put("reformPath3",list1.get(i).getReformPath3());
                map.put("reformPath4",list1.get(i).getReformPath4());
                map.put("reformPath5",list1.get(i).getReformPath5());
                map.put("reviewTime",list1.get(i).getReviewTime());
                list.add(map);
            }
        }

        return list;
    }

    @Override
    public void updateReformReview(UpdateReformReviewVO updateReformReviewVO){
        LambdaQueryWrapper<DemFireReform> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DemFireReform::getReformCode, updateReformReviewVO.getReFormId());
        List<DemFireReform> list = this.list(queryWrapper);
        DemFireReform demFireReform = new DemFireReform();
        demFireReform.setId(list.get(0).getId());
        demFireReform.setReformCode(list.get(0).getReformCode());
        demFireReform.setCompanyId(list.get(0).getCompanyId());
        demFireReform.setCompanyCode(list.get(0).getCompanyCode());
        demFireReform.setReformPart(list.get(0).getReformPart());
        demFireReform.setSendTime(list.get(0).getSendTime());
        demFireReform.setReformStatus(updateReformReviewVO.getReformStatus());
        this.updateById(demFireReform);

        LambdaQueryWrapper<DemReformReview> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.eq(DemReformReview::getReformId, list.get(0).getId());
        List<DemReformReview> list1 = demReformReviewService.list(queryWrapper1);
        DemReformReview demReformReview = new DemReformReview();
        demReformReview.setId(list1.get(0).getId());
        demReformReview.setReformId(list1.get(0).getReformId());
        demReformReview.setReformTime(list1.get(0).getReformTime());
        demReformReview.setReformPerson(list1.get(0).getReformPerson());
        demReformReview.setReformRemark(list1.get(0).getReformRemark());
        demReformReview.setReformPath1(list1.get(0).getReformPath1());
        demReformReview.setReformPath2(list1.get(0).getReformPath2());
        demReformReview.setReformPath3(list1.get(0).getReformPath3());
        demReformReview.setReformPath4(list1.get(0).getReformPath4());
        demReformReview.setReformPath5(list1.get(0).getReformPath5());
        demReformReview.setReviewTime(LocalDateTime.now());
        demReformReview.setReviewPerson(SecurityUtils.getUsername());
        demReformReview.setReviewRemark(updateReformReviewVO.getReviewRemark());
        demReformReviewService.updateById(demReformReview);

    }

    @Override
    public void updateMobileReformStatus(String reformId){
        LambdaQueryWrapper<DemFireReform> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DemFireReform::getReformCode, reformId);
        List<DemFireReform> list = this.list(queryWrapper);
        list.get(0).setReformStatus(2);
        this.updateById(list.get(0));

        LambdaQueryWrapper<DemReformReview> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.eq(DemReformReview::getReformId,list.get(0).getId());
        List<DemReformReview> list1 = demReformReviewService.list(queryWrapper1);
        if(list1.size() > 0){
            demReformReviewService.removeById(list1.get(0).getId());
        }

        DemReformReview demReformReview = new DemReformReview();
        demReformReview.setReformId(list.get(0).getId());
        demReformReview.setReformTime(LocalDateTime.now());
        demReformReviewService.save(demReformReview);
    }@Override

    public void addReformMaterial(DemReformReviewMaterialVO demReformReviewMaterialVO){
        LambdaQueryWrapper<DemFireReform> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.eq(DemFireReform::getReformCode,demReformReviewMaterialVO.getReformId());
        List<DemFireReform> list1 = this.list(queryWrapper1);
        DemFireReform demFireReform = new DemFireReform();
        demFireReform.setId(list1.get(0).getId());
        demFireReform.setReformCode(list1.get(0).getReformCode());
        demFireReform.setCompanyId(list1.get(0).getCompanyId());
        demFireReform.setCompanyCode(list1.get(0).getCompanyCode());
        demFireReform.setReformPart(list1.get(0).getReformPart());
        demFireReform.setSendTime(list1.get(0).getSendTime());
        demFireReform.setReformStatus(3);
        demFireReform.setLimitTime(list1.get(0).getLimitTime());
        this.updateById(demFireReform);

        LambdaQueryWrapper<DemReformReview> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemReformReview::getId)
                .eq(DemReformReview::getReformId, list1.get(0).getId());
        List<DemReformReview> list = demReformReviewService.list(queryWrapper);
        DemReformReview demReformReview = new DemReformReview();
        demReformReview.setId(list.get(0).getId());
        demReformReview.setReformId(list1.get(0).getId());
        demReformReview.setReformTime(LocalDateTime.now());
        demReformReview.setReformPerson(demReformReviewMaterialVO.getReformPerson());
        demReformReview.setReformRemark(demReformReviewMaterialVO.getReformRemark());
        demReformReview.setReformPath1(demReformReviewMaterialVO.getReformPath1());
        demReformReview.setReformPath2(demReformReviewMaterialVO.getReformPath2());
        demReformReview.setReformPath3(demReformReviewMaterialVO.getReformPath3());
        demReformReview.setReformPath4(demReformReviewMaterialVO.getReformPath4());
        demReformReview.setReformPath5(demReformReviewMaterialVO.getReformPath5());
        demReformReviewService.updateById(demReformReview);
    }

}
