package com.mosty.qwzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.dto.base.SysMessageInfoInsertDto;
import com.mosty.base.model.dto.base.SysMessageItemInsertDto;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.model.vo.yjzl.TbFzJlVo;
import com.mosty.base.utils.DateUtils;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.common.core.business.entity.vo.SysUserDeptVO;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import com.mosty.base.model.dto.qwzx.TbQwQwdjEditDto;
import com.mosty.base.model.entity.qwzx.TbQwQwdj;
import com.mosty.base.model.query.qwzx.TbQwQwdjQuery;
import com.mosty.qwzx.mapper.TbQwQwdjMapper;
import com.mosty.qwzx.remote.TbBaseAdaptRemoteService;
import com.mosty.qwzx.service.TbQwQwdjService;
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.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 勤务等级表 服务实现类
 * </p>
 *
 * @author zengbo
 * @since 2022-06-27
 */
@AllArgsConstructor
@Service
public class TbQwQwdjServiceImpl extends ServiceImpl<TbQwQwdjMapper, TbQwQwdj>
        implements TbQwQwdjService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public int addQwdj(TbQwQwdj qwdj) {
        UserInfo user = UserInfoManager.get();
        qwdj.setId(UUIDGenerator.getUUID());
        qwdj.setPzrId(String.valueOf(user.getUserId()));
        qwdj.setPzrSfzh(user.getIdEntityCard());
        qwdj.setPzrSsbmid(String.valueOf(user.getDeptId()));
        qwdj.setPzrSsbmdm(user.getDeptCode());
        qwdj.setPzrSsbm(user.getDeptName());
        qwdj.setPzrXm(user.getUserName());
        qwdj.setFbzt("0");
        qwdj.setXtSjly("1");
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(qwdj.getSsbmdm());
        if (dept != null) {
            qwdj.setSsbm(dept.getDeptname());
            qwdj.setSsbmid(dept.getDeptid());
            qwdj.setSsbmdm(dept.getDeptcode());
            qwdj.setSsxgajdm(dept.getFxjcode());
            qwdj.setSsxgajid(dept.getFxjid());
            qwdj.setSsxgaj(dept.getFxjname());
            qwdj.setSssgaj(dept.getDszname());
            qwdj.setSssgajdm(dept.getDszcode());
            qwdj.setSssgajid(dept.getDszid());
        }
        return this.baseMapper.insert(qwdj);
    }

    @Override
    public IPage<TbQwQwdj> getQwdj(TbQwQwdjQuery qwdj, IPage<TbQwQwdj> page) {
        QueryWrapper<TbQwQwdj> queryWrapper = new QueryWrapper<>();
//        UserInfo userInfo = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(queryWrapper, userInfo);
        qwdj.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(qwdj.getSsbmdm(), null));
        if (qwdj != null) {
            queryWrapper.lambda()
                    .likeRight(StringUtils.isNotBlank(qwdj.getSsbmdm()), TbQwQwdj::getSsbmdm, qwdj.getSsbmdm())
                    .eq(StringUtils.isNotBlank(qwdj.getQwdj()), TbQwQwdj::getQwdj, qwdj.getQwdj())
                    .eq(StringUtils.isNotBlank(qwdj.getZxfw()), TbQwQwdj::getZxfw, qwdj.getZxfw())
                    .eq(StringUtils.isNotBlank(qwdj.getFbzt()), TbQwQwdj::getFbzt, qwdj.getFbzt())
                    .eq(qwdj.getKssj() != null && qwdj.getJssj() == null, TbQwQwdj::getQwkssj, qwdj.getKssj())
                    .eq(qwdj.getKssj() == null && qwdj.getJssj() != null, TbQwQwdj::getQwjssj, qwdj.getJssj())
                    .between(qwdj.getKssj() != null && qwdj.getJssj() != null, TbQwQwdj::getQwkssj, qwdj.getKssj(), qwdj.getJssj())
//                    .between(qwdj.getKssj() != null && qwdj.getJssj() != null, TbQwQwdj::getQwjssj, qwdj.getKssj(), qwdj.getJssj())
                    .eq(TbQwQwdj::getXtScbz, "0");
        }
        return this.baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public int editQwdj(TbQwQwdjEditDto qwdj) {
        TbQwQwdj item = new TbQwQwdj();
        BeanUtils.copyProperties(qwdj, item);
//        UserInfo user = UserInfoManager.get();
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(qwdj.getSsbmdm());
        if (dept != null) {
            qwdj.setSsbm(dept.getDeptname());
            qwdj.setSsbmid(dept.getDeptid());
            qwdj.setSsbmdm(dept.getDeptcode());
            qwdj.setSsxgajdm(dept.getFxjcode());
            qwdj.setSsxgajid(dept.getFxjid());
            qwdj.setSsxgaj(dept.getFxjname());
            qwdj.setSssgaj(dept.getDszname());
            qwdj.setSssgajdm(dept.getDszcode());
            qwdj.setSssgajid(dept.getDszid());
        }
//        item.setSsbm(user.getDeptName());
//        item.setSsbmid(String.valueOf(user.getDeptId()));
//        item.setSsbmdm(user.getDeptCode());
//        item.setSsxgaj(user.getFxjDeptName());
//        item.setSsxgajid(String.valueOf(user.getFxjDeptId()));
//        item.setSsxgajdm(user.getFxjDeptCode());
//        item.setSssgaj(user.getDszDeptName());
//        item.setSssgajid(String.valueOf(user.getDszDeptId()));
//        item.setSssgajdm(user.getDszDeptCode());
        return this.baseMapper.updateById(item);
    }

    @Override
    public boolean deleteBatch(List<String> ids) {
        if (ids.size() > 0) {
            int rs = this.baseMapper.deleteQwdj(ids);
            return rs > 0;
        }
        return true;
    }

    @Override
    public boolean deleteById(String id) {
        List<String> ids = new ArrayList<>();
        ids.add(id);
        int rs = this.baseMapper.deleteQwdj(ids);
        return rs > 0;
    }

    @Override
    public TbQwQwdj selectById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public boolean publishQwdj(String id) {
        TbQwQwdj qwdj = this.baseMapper.selectById(id);
        qwdj.setFbsj(new Date());
        qwdj.setFbzt("1");
        int rs = this.baseMapper.updateById(qwdj);
        if (rs > 0) {
            //发布任务以后通知人员信息 数据较多 使用线程方式下发
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //获取部门下面的所有人员
                    List<SysUserDeptVO> list = new ArrayList<>();
                    if (StringUtils.isNotEmpty(qwdj.getSfxj()) && qwdj.getSfxj().equals("1")) {
                        list = tbBaseAdaptRemoteService.getUserAll(qwdj.getSsbmid());
                    } else {
                        list = tbBaseAdaptRemoteService.getUserList(qwdj.getSsbmid());
                    }
                    if (!CollectionUtils.isEmpty(list)) {
                        SysMessageInfoInsertDto dto = new SysMessageInfoInsertDto();
                        List<SysMessageItemInsertDto> itemList = new ArrayList<>();
                        list.forEach(item -> {
                            SysMessageItemInsertDto itemDto = new SysMessageItemInsertDto();
                            itemDto.setXxjsrXm(item.getUserName());
                            itemDto.setXxjsrSfzh(item.getIdEntityCard());
                            itemDto.setXxjsrId(String.valueOf(item.getId()));
                            itemDto.setXxjsbm(qwdj.getSsbm());
                            itemDto.setXxjsbmId(qwdj.getSsbmid());
                            itemList.add(itemDto);
                        });
                        if (itemList.size() > 0) {
                            dto.setItemList(itemList);
                            String xxnr = qwdj.getPzrSsbm() + "（" + qwdj.getPzrXm() + "）下发:" + qwdj.getQwdjmc() + ",勤务时间：" + DateUtils.getQueryDateString(qwdj.getQwkssj(), "02") + "-" + DateUtils.getQueryDateString(qwdj.getQwjssj(), "02") + ",请注意查收！";
                            dto.setXxnr(xxnr);
                            dto.setXxlx("1");
                            dto.setXxzt("2");
                            tbBaseAdaptRemoteService.sendMsg(dto);
                        }
                    }
                }
            }).start();
        }
        return rs > 0;
    }

    @Override
    public TbQwQwdj queryCurrentQwdj(String dateTime) {
        return this.baseMapper.queryCurrentQwdj(dateTime);
    }
}
