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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.entity.yjzl.TbYjxx;
import com.mosty.base.model.entity.yjzl.TbYjxxSj;
import com.mosty.base.model.entity.yjzl.TbYjxxSjGlrycl;
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.TbYjxxSjQuery;
import com.mosty.base.model.vo.yjzl.TbYjxxSjVo;
import com.mosty.yjzl.mapper.TbYjxxMapper;
import com.mosty.yjzl.mapper.TbYjxxSjGlryclMapper;
import com.mosty.yjzl.mapper.TbYjxxSjMapper;
import com.mosty.yjzl.remote.TbBaseAdaptRemoteService;
import com.mosty.yjzl.service.TbYjxxSjService;
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 TbYjxxSjServiceImpl extends ServiceImpl<TbYjxxSjMapper, TbYjxxSj>
        implements TbYjxxSjService {

    private final TbYjxxSjGlryclMapper tbYjxxSjGlryclMapper;
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public IPage<TbYjxxSjVo> queryByPage(TbYjxxSjQuery yjxxSj) {
        IPage<TbYjxxSjVo> voIPage = new Page<>(yjxxSj.getPageCurrent(), yjxxSj.getPageSize());
        IPage<TbYjxxSj> pageQuery = new Page<>(yjxxSj.getPageCurrent(), yjxxSj.getPageSize());
        QueryWrapper<TbYjxxSj> queryWrapper = getQueryWrapper(yjxxSj);
        IPage<TbYjxxSj> page = this.baseMapper.selectPage(pageQuery, queryWrapper);
        voIPage.setTotal(page.getTotal());
        List<TbYjxxSj> records = page.getRecords();
        List<TbYjxxSjVo> vos = dataPerfact(records);
        voIPage.setRecords(vos);
        return voIPage;
    }

    @Override
    public List<TbYjxxSjVo> queryList(TbYjxxSjQuery tbYjxxSjQuery) {
        QueryWrapper<TbYjxxSj> queryWrapper = getQueryWrapper(tbYjxxSjQuery);
        List<TbYjxxSj> records = this.baseMapper.selectList(queryWrapper);
        List<TbYjxxSjVo> vos = dataPerfact(records);
        return vos;
    }

    @Override
    public int insert(TbYjxxSjVo sjVo) {
        TbYjxxSj sj = new TbYjxxSj();
        BeanUtils.copyProperties(sjVo, sj);
        sj.setId(UUIDGenerator.getUUID());
        perfectDept(sj);
        saveRelevanceData(sj, sjVo);
        return this.baseMapper.insert(sj);
    }

    @Override
    public int update(TbYjxxSjVo sjVo) {
        TbYjxxSj sj = new TbYjxxSj();
        BeanUtils.copyProperties(sjVo, sj);
        sj.setXtZhgxsj(new Timestamp(System.currentTimeMillis()));
        tbYjxxSjGlryclMapper.deleteByYjId(sj.getId());
        saveRelevanceData(sj, sjVo);
        return this.baseMapper.updateById(sj);
    }

    @Override
    public int deleteById(List<String> list) {
        return this.baseMapper.deleteByIds(list);
    }

    @Override
    public TbYjxxSjVo queryById(String id) {
        TbYjxxSjVo vo = new TbYjxxSjVo();
        TbYjxxSj sj = this.baseMapper.selectById(id);
        BeanUtils.copyProperties(sj, vo);
        List<TbYjxxSjGlrycl> glryclList = tbYjxxSjGlryclMapper.queryByYjid(Arrays.asList(id));
        vo.setGlryclList(glryclList);
        return vo;
    }

    @Override
    public void addEntity(TbYjxx yjxx, String sjFl, String sjFlmc) {
        TbYjxxSj sj = new TbYjxxSj();
        BeanUtils.copyProperties(yjxx, sj);
        sj.setId(UUIDGenerator.getUUID());
        sj.setYjId(yjxx.getId());
        sj.setSjFl(sjFl);
        sj.setXtSjly("1");
        sj.setSjFlmc(sjFlmc);
        this.baseMapper.insertEntity(sj);
    }

    /**
     * 获取查询条件
     *
     * @param yjsj
     * @return
     */
    public QueryWrapper<TbYjxxSj> getQueryWrapper(TbYjxxSjQuery yjsj) {
        QueryWrapper<TbYjxxSj> queryWrapper = new QueryWrapper<>();
//        UserInfo userInfo = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(queryWrapper, userInfo);
        String ssbm = this.tbBaseAdaptRemoteService.getSsbm(null, null);
        queryWrapper.lambda().eq(TbYjxxSj::getXtScbz, "0")
        .eq(StringUtils.isNotBlank(yjsj.getYjId()), TbYjxxSj::getYjId, yjsj.getYjId())
        .like(StringUtils.isNotBlank(yjsj.getYjGzyid()), TbYjxxSj::getYjGzyid, yjsj.getYjGzyid())
        .likeRight(StringUtils.isNotBlank(ssbm), TbYjxxSj::getSsbmdm, ssbm)
        .eq(StringUtils.isNotBlank(yjsj.getSjFl()), TbYjxxSj::getSjFl, yjsj.getSjFl())
        .eq(StringUtils.isNotBlank(yjsj.getYjJb()), TbYjxxSj::getYjJb, yjsj.getYjJb());
//        if (yjsj.getYjsksj() != null && yjsj.getYjsksj() == null) {
//            queryWrapper.lambda().ge(TbYjxxSj::getYjSj, yjsj.getYjsksj());
//        }
//        if (yjsj.getYjsksj() == null && yjsj.getYjsksj() != null) {
//            queryWrapper.lambda().le(TbYjxxSj::getYjSj, yjsj.getYjsksj());
//        }
        if (yjsj.getYjsksj() != null && yjsj.getYjsksj() != null) {
            queryWrapper.lambda().between(TbYjxxSj::getYjSj, yjsj.getYjsksj(), yjsj.getYjsksj());
        }
        return queryWrapper;
    }

    /**
     * 预警人员数据完善
     *
     * @param records
     * @return
     */
    public List<TbYjxxSjVo> dataPerfact(List<TbYjxxSj> records) {
        List<TbYjxxSjVo> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(records)) {
            List<String> ids = records.stream().map(TbYjxxSj::getId).collect(Collectors.toList());
            List<TbYjxxSjGlrycl> bqList = tbYjxxSjGlryclMapper.queryByYjid(ids);
            for (TbYjxxSj record : records) {
                TbYjxxSjVo vo = new TbYjxxSjVo();
                BeanUtils.copyProperties(record, vo);
                List<TbYjxxSjGlrycl> yjczjl =
                        bqList.stream().filter(item -> item.getYjId().equals(record.getId())).collect(Collectors.toList());
                vo.setGlryclList(yjczjl);
                vos.add(vo);
            }
        }
        return vos;
    }

    /**
     * 部门赋值
     *
     * @param zfpb
     */
    public void perfectDept(TbYjxxSj 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());
    }

    /**
     * 保存关联数据
     *
     * @param ry
     * @param vo
     */
    public void saveRelevanceData(TbYjxxSj ry, TbYjxxSjVo vo) {
        if (vo != null) {
            List<TbYjxxSjGlrycl> sjList = vo.getGlryclList();
            if (!CollectionUtils.isEmpty(sjList)) {
                for (TbYjxxSjGlrycl sj : sjList) {
                    sj.setId(UUIDGenerator.getUUID());
                    sj.setYjId(ry.getYjId());
                    sj.setYjryclId(ry.getId());
                    sj.setXtSjzt("1");
                    sj.setXtSjly("1");
                    sj.setXtScbz("0");
                    tbYjxxSjGlryclMapper.insert(sj);
                }
            }
        }
    }


}
