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.TbYjxxCl;
import com.mosty.base.model.entity.yjzl.TbYjxxClBq;
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.TbYjxxClQuery;
import com.mosty.base.model.vo.yjzl.TbYjxxClVo;
import com.mosty.yjzl.mapper.TbYjxxClBqMapper;
import com.mosty.yjzl.mapper.TbYjxxClMapper;
import com.mosty.yjzl.remote.TbBaseAdaptRemoteService;
import com.mosty.yjzl.service.TbYjxxClService;
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-21
 */
@AllArgsConstructor
@Service
public class TbYjxxClServiceImpl implements TbYjxxClService {

    private final TbYjxxClMapper tbYjxxClMapper;
    private final TbYjxxClBqMapper tbYjxxClBqMapper;
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public IPage<TbYjxxClVo> queryByPage(TbYjxxClQuery yjcl) {
        IPage<TbYjxxClVo> voIPage = new Page<>(yjcl.getPageCurrent(), yjcl.getPageSize());
        IPage<TbYjxxCl> pageQuery = new Page<>(yjcl.getPageCurrent(), yjcl.getPageSize());
        QueryWrapper<TbYjxxCl> queryWrapper = getQueryWrapper(yjcl);
        IPage<TbYjxxCl> page = tbYjxxClMapper.selectPage(pageQuery, queryWrapper);
        voIPage.setTotal(page.getTotal());
        List<TbYjxxCl> records = page.getRecords();
        List<TbYjxxClVo> vos = dataPerfact(records);
        voIPage.setRecords(vos);
        return voIPage;
    }

    @Override
    public List<TbYjxxClVo> queryList(TbYjxxClQuery tbYjxxClQuery) {
        QueryWrapper<TbYjxxCl> queryWrapper = getQueryWrapper(tbYjxxClQuery);
        List<TbYjxxCl> records = tbYjxxClMapper.selectList(queryWrapper);
        List<TbYjxxClVo> vos = dataPerfact(records);
        return vos;
    }

    @Override
    public int insert(TbYjxxClVo clVo) {
        TbYjxxCl cl = new TbYjxxCl();
        BeanUtils.copyProperties(clVo, cl);
        cl.setId(UUIDGenerator.getUUID());
        perfectDept(cl);
        saveRelevanceData(cl, clVo);
        return tbYjxxClMapper.insert(cl);
    }

    @Override
    public int update(TbYjxxClVo clVo) {
        TbYjxxCl cl = new TbYjxxCl();
        BeanUtils.copyProperties(clVo, cl);
        cl.setXtZhgxsj(new Timestamp(System.currentTimeMillis()));
        tbYjxxClBqMapper.deleteByYjId(cl.getId());
        saveRelevanceData(cl, clVo);
        return tbYjxxClMapper.updateById(cl);
    }

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

    @Override
    public TbYjxxClVo queryById(String id) {
        TbYjxxCl cl = tbYjxxClMapper.selectById(id);
        TbYjxxClVo vo = new TbYjxxClVo();
        BeanUtils.copyProperties(cl, vo);
        List<TbYjxxClBq> bqList = tbYjxxClBqMapper.queryByYjid(Arrays.asList(id));
        vo.setClbqList(bqList);
        return vo;
    }

    /**
     * 获取查询条件
     *
     * @param yjcl
     * @return
     */
    public QueryWrapper<TbYjxxCl> getQueryWrapper(TbYjxxClQuery yjcl) {
        QueryWrapper<TbYjxxCl> queryWrapper = new QueryWrapper<>();
//        UserInfo userInfo = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(queryWrapper, userInfo);

        yjcl.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(yjcl.getSsbmdm(), null));
        queryWrapper.lambda().eq(TbYjxxCl::getXtScbz, "0")
                .eq(StringUtils.isNotBlank(yjcl.getYjId()), TbYjxxCl::getYjId, yjcl.getYjId())
                .eq(StringUtils.isNotBlank(yjcl.getSjId()), TbYjxxCl::getSjId, yjcl.getSjId())
                .eq(StringUtils.isNotBlank(yjcl.getYjClbq()), TbYjxxCl::getYjClbq, yjcl.getYjClbq())
                .eq(StringUtils.isNotBlank(yjcl.getYjJb()), TbYjxxCl::getYjJb, yjcl.getYjJb())
                .like(StringUtils.isNotBlank(yjcl.getYjClcph()), TbYjxxCl::getYjClcph, yjcl.getYjClcph())
                .likeRight(StringUtils.isNotBlank(yjcl.getSsbmdm()), TbYjxxCl::getSsbmdm, yjcl.getSsbmdm());
        if (yjcl.getYjsksj() != null && yjcl.getYjsksj() == null) {
            queryWrapper.lambda().ge(TbYjxxCl::getYjSj, yjcl.getYjsksj());
        }
        if (yjcl.getYjsksj() == null && yjcl.getYjsksj() != null) {
            queryWrapper.lambda().le(TbYjxxCl::getYjSj, yjcl.getYjsksj());
        }
        if (yjcl.getYjsksj() != null && yjcl.getYjsksj() != null) {
            queryWrapper.lambda().between(TbYjxxCl::getYjSj, yjcl.getYjsksj(), yjcl.getYjsksj());
        }
        return queryWrapper;
    }

    /**
     * 预警人员数据完善
     *
     * @param records
     * @return
     */
    public List<TbYjxxClVo> dataPerfact(List<TbYjxxCl> records) {
        List<TbYjxxClVo> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(records)) {
            List<String> ids = records.stream().map(TbYjxxCl::getId).collect(Collectors.toList());
            List<TbYjxxClBq> bqList = tbYjxxClBqMapper.queryByYjid(ids);
            for (TbYjxxCl record : records) {
                TbYjxxClVo vo = new TbYjxxClVo();
                BeanUtils.copyProperties(record, vo);
                List<TbYjxxClBq> clBqList =
                        bqList.stream().filter(item -> item.getYjclId().equals(record.getId())).collect(Collectors.toList());
                vo.setClbqList(clBqList);
                vos.add(vo);
            }
        }
        return vos;
    }

    /**
     * 保存关联数据
     *
     * @param cl
     * @param vo
     */
    public void saveRelevanceData(TbYjxxCl cl, TbYjxxClVo vo) {
        if (vo != null) {
            List<TbYjxxClBq> clList = vo.getClbqList();
            if (!CollectionUtils.isEmpty(clList)) {
                for (TbYjxxClBq bq : clList) {
                    bq.setId(UUIDGenerator.getUUID());
                    bq.setYjclId(cl.getId());
                    bq.setXtSjzt("1");
                    bq.setXtSjly("1");
                    bq.setXtScbz("0");
                    tbYjxxClBqMapper.insert(bq);
                }
            }
        }
    }

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

}
