package com.mosty.yjzl.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.base.model.entity.yjzl.*;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import com.mosty.base.model.query.yjzl.TbYjxxDypzQuery;
import com.mosty.base.model.vo.yjzl.TbYjxxDypzVo;
import com.mosty.yjzl.mapper.*;
import com.mosty.yjzl.remote.TbBaseAdaptRemoteService;
import com.mosty.yjzl.service.TbYjxxDypzService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 预警信息订阅配置
 * </p>
 *
 * @author zengbo
 * @since 2022-07-22
 */
@AllArgsConstructor
@Service
public class TbYjxxDypzServiceImpl implements TbYjxxDypzService {

    private TbYjxxDypzMapper tbYjxxDypzMapper;

    private TbYjxxDypzClbqMapper tbYjxxDypzClbqMapper;

    private TbYjxxDypzGzyMapper tbYjxxDypzGzyMapper;

    private TbYjxxDypzRybqMapper tbYjxxDypzRybqMapper;

    private TbYjxxDypzSjbqMapper tbYjxxDypzSjbqMapper;

    private TbYjxxDypzYjjbMapper tbYjxxDypzYjjbMapper;

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public IPage<TbYjxxDypzVo> queryList(TbYjxxDypzQuery dypz) {
        IPage<TbYjxxDypz> xfpbIPage = queryDypz(dypz);
        IPage<TbYjxxDypzVo> voIPage = new Page<>(dypz.getPageCurrent(), dypz.getPageSize());
        List<TbYjxxDypz> records = xfpbIPage.getRecords();
        List<TbYjxxDypzVo> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(records)) {
            List<String> ids = records.stream().map(TbYjxxDypz::getId).collect(Collectors.toList());
            List<TbYjxxDypzClbq> clbqList = tbYjxxDypzClbqMapper.queryByDyid(ids);
            List<TbYjxxDypzGzy> gzyList = tbYjxxDypzGzyMapper.queryByDyid(ids);
            List<TbYjxxDypzRybq> rybqList = tbYjxxDypzRybqMapper.queryByDyid(ids);
            List<TbYjxxDypzSjbq> sjbqList = tbYjxxDypzSjbqMapper.queryByDyid(ids);
            List<TbYjxxDypzYjjb> yjjbList = tbYjxxDypzYjjbMapper.queryByDyid(ids);
            for (TbYjxxDypz record : records) {
                TbYjxxDypzVo vo = new TbYjxxDypzVo();
                BeanUtils.copyProperties(record, vo);
                List<TbYjxxDypzClbq> clbq = clbqList.stream().filter(item -> item.getDyId().equals(record.getId())).collect(Collectors.toList());
                List<TbYjxxDypzGzy> gzy = gzyList.stream().filter(item -> item.getDyId().equals(record.getId())).collect(Collectors.toList());
                List<TbYjxxDypzRybq> rybq = rybqList.stream().filter(item -> item.getDyId().equals(record.getId())).collect(Collectors.toList());
                List<TbYjxxDypzSjbq> sjbq = sjbqList.stream().filter(item -> item.getDyId().equals(record.getId())).collect(Collectors.toList());
                List<TbYjxxDypzYjjb> yjjb = yjjbList.stream().filter(item -> item.getDyId().equals(record.getId())).collect(Collectors.toList());
                vo.setClbqList(clbq);
                vo.setGzyList(gzy);
                vo.setRybqList(rybq);
                vo.setSjbqList(sjbq);
                vo.setYjjbList(yjjb);
                vos.add(vo);
            }
        }
        voIPage.setRecords(vos);
        voIPage.setTotal(xfpbIPage.getTotal());
        return voIPage;
    }

    @Override
    public int insert(TbYjxxDypzVo dypzVo) {
        TbYjxxDypz dypz = new TbYjxxDypz();
        BeanUtils.copyProperties(dypzVo, dypz);
        dypz.setId(UUIDGenerator.getUUID());
        UserInfo user = UserInfoManager.get();
        dypz.setSsbm(user.getDeptName());
        dypz.setSsbmid(String.valueOf(user.getDeptId()));
        dypz.setSsbmdm(user.getDeptCode());
        dypz.setSsxgaj(user.getFxjDeptName());
        dypz.setSsxgajid(String.valueOf(user.getFxjDeptId()));
        dypz.setSsxgajdm(user.getFxjDeptCode());
        dypz.setSssgaj(user.getDszDeptName());
        dypz.setSssgajid(String.valueOf(user.getDszDeptId()));
        dypz.setSssgajdm(user.getDszDeptCode());
        saveRelevanceData(dypzVo, dypz.getId());
        return tbYjxxDypzMapper.insert(dypz);
    }

    @Override
    public int updateById(TbYjxxDypzVo dypzVo) {
        TbYjxxDypz dypz = new TbYjxxDypz();
        BeanUtils.copyProperties(dypzVo, dypz);
        dypz.setXtZhgxsj(new Timestamp(System.currentTimeMillis()));
        deleteRelevanceData(dypz.getId());
        saveRelevanceData(dypzVo, dypz.getId());
        return tbYjxxDypzMapper.updateById(dypz);
    }

    @Override
    public TbYjxxDypzVo queryById(String id) {
        TbYjxxDypz dypz = tbYjxxDypzMapper.selectById(id);
        TbYjxxDypzVo vo = new TbYjxxDypzVo();
        BeanUtils.copyProperties(dypz, vo);
        List<TbYjxxDypzClbq> clbqList = tbYjxxDypzClbqMapper.queryByDyid(Collections.singletonList(id));
        List<TbYjxxDypzGzy> gzyList = tbYjxxDypzGzyMapper.queryByDyid(Collections.singletonList(id));
        List<TbYjxxDypzRybq> rybqList = tbYjxxDypzRybqMapper.queryByDyid(Collections.singletonList(id));
        List<TbYjxxDypzSjbq> sjbqList = tbYjxxDypzSjbqMapper.queryByDyid(Collections.singletonList(id));
        List<TbYjxxDypzYjjb> yjjbList = tbYjxxDypzYjjbMapper.queryByDyid(Collections.singletonList(id));
        vo.setClbqList(clbqList);
        vo.setGzyList(gzyList);
        vo.setRybqList(rybqList);
        vo.setSjbqList(sjbqList);
        vo.setYjjbList(yjjbList);
        return vo;
    }

    @Override
    public int deleteById(List<String> list) {
        if (list.size() > 0) {
            for (String id : list) {
                deleteRelevanceData(id);
            }
        }
        return tbYjxxDypzMapper.deleteByIds(list);
    }

    public IPage<TbYjxxDypz> queryDypz(TbYjxxDypzQuery dypz) {
        IPage<TbYjxxDypz> page = new Page<>(dypz.getPageCurrent(), dypz.getPageSize());
        QueryWrapper<TbYjxxDypz> qw = new QueryWrapper<>();
//        UserInfo userInfo = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(qw, userInfo);
        dypz.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dypz.getSsbmdm(), null));
        qw.lambda().eq(StringUtils.isNotBlank(dypz.getDyryId()), TbYjxxDypz::getDyryId, dypz.getDyryId())
                .like(StringUtils.isNotBlank(dypz.getDyryXm()), TbYjxxDypz::getDyryXm, dypz.getDyryXm())
                .eq(StringUtils.isNotBlank(dypz.getDyYjjb()), TbYjxxDypz::getDyYjjb, dypz.getDyYjjb())
                .eq(StringUtils.isNotBlank(dypz.getDyYjrybq()), TbYjxxDypz::getDyYjrybq, dypz.getDyYjrybq())
                .like(StringUtils.isNotBlank(dypz.getDyYjclbq()), TbYjxxDypz::getDyYjclbq, dypz.getDyYjclbq())
                .like(StringUtils.isNotBlank(dypz.getSsbmdm()), TbYjxxDypz::getSsbmdm, dypz.getSsbmdm())
                .likeRight(StringUtils.isNotBlank(dypz.getSsbmdm()), TbYjxxDypz::getSsbmdm, dypz.getSsbmdm())
                .like(StringUtils.isNotBlank(dypz.getSsbmid()), TbYjxxDypz::getSsbmid, dypz.getSsbmid());
        return tbYjxxDypzMapper.selectPage(page, qw);
    }

    // 保存关联数据
    public void saveRelevanceData(TbYjxxDypzVo vo, String ywid) {
        if (vo != null) {
            List<TbYjxxDypzClbq> clbqList = vo.getClbqList();
            List<TbYjxxDypzGzy> gzyList = vo.getGzyList();
            List<TbYjxxDypzRybq> rybqList = vo.getRybqList();
            List<TbYjxxDypzSjbq> sjbqList = vo.getSjbqList();
            List<TbYjxxDypzYjjb> yjjbList = vo.getYjjbList();
            if (!CollectionUtils.isEmpty(clbqList)) {
                for (TbYjxxDypzClbq clbq : clbqList) {
                    clbq.setId(UUIDGenerator.getUUID());
                    clbq.setDyId(ywid);
                    clbq.setXtSjly("1");
                    tbYjxxDypzClbqMapper.insert(clbq);
                }
            }
            if (!CollectionUtils.isEmpty(gzyList)) {
                for (TbYjxxDypzGzy gzy : gzyList) {
                    gzy.setId(UUIDGenerator.getUUID());
                    gzy.setDyId(ywid);
                    gzy.setXtSjly("1");
                    tbYjxxDypzGzyMapper.insert(gzy);
                }
            }
            if (!CollectionUtils.isEmpty(rybqList)) {
                for (TbYjxxDypzRybq rybq : rybqList) {
                    rybq.setId(UUIDGenerator.getUUID());
                    rybq.setDyId(ywid);
                    rybq.setXtSjly("1");
                    tbYjxxDypzRybqMapper.insert(rybq);
                }
            }
            if (!CollectionUtils.isEmpty(sjbqList)) {
                for (TbYjxxDypzSjbq sjbq : sjbqList) {
                    sjbq.setId(UUIDGenerator.getUUID());
                    sjbq.setDyId(ywid);
                    sjbq.setXtSjly("1");
                    tbYjxxDypzSjbqMapper.insert(sjbq);
                }
            }
            if (!CollectionUtils.isEmpty(yjjbList)) {
                for (TbYjxxDypzYjjb yjjb : yjjbList) {
                    yjjb.setId(UUIDGenerator.getUUID());
                    yjjb.setDyId(ywid);
                    yjjb.setXtSjly("1");
                    tbYjxxDypzYjjbMapper.insert(yjjb);
                }
            }
        }
    }

    // 删除关联数据
    public void deleteRelevanceData(String id) {
        tbYjxxDypzClbqMapper.deleteByDyid(id);
        tbYjxxDypzGzyMapper.deleteByDyid(id);
        tbYjxxDypzRybqMapper.deleteByDyid(id);
        tbYjxxDypzSjbqMapper.deleteByDyid(id);
        tbYjxxDypzYjjbMapper.deleteByDyid(id);
    }
}
