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.model.entity.yjzl.TbYjxxRy;
import com.mosty.base.model.entity.yjzl.TbYjxxRyBq;
import com.mosty.base.model.entity.yjzl.TbYjxxRySjaj;
import com.mosty.base.utils.UUIDGenerator;
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.TbYjxxRyQuery;
import com.mosty.base.model.vo.yjzl.TbYjxxRyVo;
import com.mosty.yjzl.mapper.TbYjxxRyBqMapper;
import com.mosty.yjzl.mapper.TbYjxxRyMapper;
import com.mosty.yjzl.mapper.TbYjxxRySjajMapper;
import com.mosty.yjzl.remote.TbBaseAdaptRemoteService;
import com.mosty.yjzl.service.TbYjxxRyService;
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.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 预警信息-人员表 服务实现类
 * </p>
 *
 * @author zengbo
 * @since 2022-07-19
 */
@AllArgsConstructor
@Service
public class TbYjxxRyServiceImpl implements TbYjxxRyService {

    private final TbYjxxRyMapper tbYjxxRyMapper;
    private final TbYjxxRyBqMapper tbYjxxRyBqMapper;
    private final TbYjxxRySjajMapper tbYjxxRySjajMapper;
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public IPage<TbYjxxRyVo> queryByPage(TbYjxxRyQuery yjry) {
        IPage<TbYjxxRyVo> voIPage = new Page<>(yjry.getPageCurrent(), yjry.getPageSize());
        IPage<TbYjxxRy> pageQuery = new Page<>(yjry.getPageCurrent(), yjry.getPageSize());
        QueryWrapper<TbYjxxRy> queryWrapper = getQueryWrapper(yjry);
        IPage<TbYjxxRy> page = tbYjxxRyMapper.selectPage(pageQuery, queryWrapper);
        voIPage.setTotal(page.getTotal());
        List<TbYjxxRy> records = page.getRecords();
        List<TbYjxxRyVo> vos = dataPerfact(records);
        voIPage.setRecords(vos);
        return voIPage;
    }

    @Override
    public List<TbYjxxRyVo> queryList(TbYjxxRyQuery tbYjxxRyQuery) {
        QueryWrapper<TbYjxxRy> queryWrapper = getQueryWrapper(tbYjxxRyQuery);
        List<TbYjxxRy> records = tbYjxxRyMapper.selectList(queryWrapper);
        return dataPerfact(records);
    }

    @Override
    public int insert(TbYjxxRyVo ryVo) {
        TbYjxxRy ry = new TbYjxxRy();
        BeanUtils.copyProperties(ryVo, ry);
        ry.setId(UUIDGenerator.getUUID());
        perfectDept(ry);
        saveRelevanceData(ry, ryVo, ry.getId());
        return tbYjxxRyMapper.insert(ry);
    }

    @Override
    public int update(TbYjxxRyVo ryVo) {
        TbYjxxRy ry = new TbYjxxRy();
        BeanUtils.copyProperties(ryVo, ry);
        ry.setXtZhgxsj(new Timestamp(System.currentTimeMillis()));
        deleteRelevanceData(ry.getId());
        saveRelevanceData(ry, ryVo, ry.getId());
        return tbYjxxRyMapper.updateById(ry);
    }

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

    @Override
    public TbYjxxRyVo queryById(String id) {
        TbYjxxRyVo ryVo = new TbYjxxRyVo();
        TbYjxxRy ry = tbYjxxRyMapper.selectById(id);
        BeanUtils.copyProperties(ry, ryVo);
        List<TbYjxxRyBq> bqList = tbYjxxRyBqMapper.queryByYjryId(Arrays.asList(id));
        List<TbYjxxRySjaj> sjajList = tbYjxxRySjajMapper.queryByYjryId(Arrays.asList((id)));
        ryVo.setRybqList(bqList);
        ryVo.setSjajList(sjajList);
        return ryVo;
    }

    /**
     * 获取查询条件
     *
     * @param yjry
     * @return
     */
    public QueryWrapper<TbYjxxRy> getQueryWrapper(TbYjxxRyQuery yjry) {
        QueryWrapper<TbYjxxRy> queryWrapper = new QueryWrapper<>();
//        UserInfo userInfo = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(queryWrapper, userInfo);
        yjry.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(yjry.getSsbmdm(), null));
        queryWrapper.lambda()
                .eq(TbYjxxRy::getXtScbz, "0")
                .eq(StringUtils.isNotBlank(yjry.getYjId()), TbYjxxRy::getYjId, yjry.getYjId())
                .like(StringUtils.isNotBlank(yjry.getYjClid()), TbYjxxRy::getYjClid, yjry.getYjClid())
                .likeRight(StringUtils.isNotBlank(yjry.getSsbmdm()), TbYjxxRy::getSsbmdm, yjry.getSsbmdm())
                .eq(StringUtils.isNotBlank(yjry.getSjId()), TbYjxxRy::getSjId, yjry.getSjId())
                .eq(StringUtils.isNotBlank(yjry.getYjSjly()), TbYjxxRy::getYjSjly, yjry.getYjSjly())
                .like(StringUtils.isNotBlank(yjry.getYjGzyid()), TbYjxxRy::getYjGzyid, yjry.getYjGzyid())
                .like(StringUtils.isNotBlank(yjry.getYjJb()), TbYjxxRy::getYjJb, yjry.getYjJb());
        return queryWrapper;
    }

    /**
     * 预警人员数据完善
     *
     * @param records
     * @return
     */
    public List<TbYjxxRyVo> dataPerfact(List<TbYjxxRy> records) {
        List<TbYjxxRyVo> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(records)) {
            List<String> ids = records.stream().map(TbYjxxRy::getId).collect(Collectors.toList());
            List<TbYjxxRyBq> bqList = tbYjxxRyBqMapper.queryByYjryId(ids);
            List<TbYjxxRySjaj> sjajList = tbYjxxRySjajMapper.queryByYjryId(ids);
            for (TbYjxxRy record : records) {
                TbYjxxRyVo vo = new TbYjxxRyVo();
                BeanUtils.copyProperties(record, vo);
                List<TbYjxxRyBq> bq =
                        bqList.stream().filter(item -> item.getYjryId().equals(record.getId())).collect(Collectors.toList());
                List<TbYjxxRySjaj> sjaj =
                        sjajList.stream().filter(item -> item.getYjryId().equals(record.getId())).collect(Collectors.toList());
                vo.setRybqList(bq);
                vo.setSjajList(sjaj);
                vos.add(vo);
            }
        }
        return vos;
    }

    /**
     * 保存关联数据
     *
     * @param vo
     * @param ywid
     */
    public void saveRelevanceData(TbYjxxRy ry, TbYjxxRyVo vo, String ywid) {
        if (vo != null) {
            List<TbYjxxRyBq> bqList = vo.getRybqList();
            List<TbYjxxRySjaj> sjajList = vo.getSjajList();
            if (!CollectionUtils.isEmpty(bqList)) {
                for (TbYjxxRyBq bq : bqList) {
                    bq.setId(UUIDGenerator.getUUID());
                    bq.setYjryId(ywid);
                    bq.setXtSjzt("1");
                    bq.setXtSjly("1");
                    bq.setXtScbz("0");
                    tbYjxxRyBqMapper.insert(bq);
                }
            }
            if (!CollectionUtils.isEmpty(sjajList)) {
                for (TbYjxxRySjaj sjaj : sjajList) {
                    sjaj.setId(UUIDGenerator.getUUID());
                    sjaj.setYjryId(ywid);
                    sjaj.setXtSjzt("1");
                    sjaj.setXtSjly("1");
                    sjaj.setXtScbz("0");
                    tbYjxxRySjajMapper.insert(sjaj);
                }
            }
        }
    }

    /**
     * 删除关联数据
     *
     * @param id
     */
    public void deleteRelevanceData(String id) {
        tbYjxxRyBqMapper.deleteByYwid(id);
        tbYjxxRySjajMapper.deleteByYwid(id);
    }

    /**
     * 部门赋值
     *
     * @param zfpb
     */
    public void perfectDept(TbYjxxRy zfpb) {
        zfpb.setXtSjly("1");
        zfpb.setXtSjzt("1");
        zfpb.setXtScbz("0");
        UserInfo user = UserInfoManager.get();
        zfpb.setSsbm(user.getDeptName());
        zfpb.setSsbmid(String.valueOf(user.getDeptId()));
        zfpb.setSsbmdm(user.getDeptCode());
        zfpb.setSsxgaj(user.getFxjDeptName());
        zfpb.setSsxgajid(String.valueOf(user.getFxjDeptId()));
        zfpb.setSsxgajdm(user.getFxjDeptCode());
        zfpb.setSssgaj(user.getDszDeptName());
        zfpb.setSssgajid(String.valueOf(user.getDszDeptId()));
        zfpb.setSssgajdm(user.getDszDeptCode());
    }

}
