package com.mosty.common.task.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.mosty.common.base.constant.enums.DeletedEnum;
import com.mosty.common.base.exception.Asserts;
import com.mosty.common.base.util.DateUtils;
import com.mosty.common.base.util.LocalDateTimeUtils;
import com.mosty.common.base.util.StringUtils;
import com.mosty.common.core.business.entity.SysDept;
import com.mosty.common.core.business.entity.SysUser;
import com.mosty.common.core.business.service.SysDeptService;
import com.mosty.common.core.business.service.SysUserService;
import com.mosty.common.core.login.dto.DeptDTO;
import com.mosty.common.task.constant.enums.*;
import com.mosty.common.task.entity.SysTask;
import com.mosty.common.task.entity.SysTaskItem;
import com.mosty.common.task.entity.SysTaskLog;
import com.mosty.common.task.entity.vo.*;
import com.mosty.common.task.service.SysTaskItemService;
import com.mosty.common.task.service.SysTaskLogService;
import com.mosty.common.task.service.SysTaskService;
import com.mosty.common.task.mapper.SysTaskMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author liyan
 * @description 针对表【sys_task(任务表)】的数据库操作Service实现
 * @createDate 2022-04-21 21:31:14
 */
@Service
public class SysTaskServiceImpl extends ServiceImpl<SysTaskMapper, SysTask>
        implements SysTaskService {
    @Resource
    private SysTaskMapper sysTaskMapper;
    @Resource
    private SysTaskItemService sysTaskItemService;
    @Resource
    private SysTaskLogService sysTaskLogService;
    @Resource
    private SysUserService userService;
    @Resource
    private SysDeptService sysDeptService;

    @Override
    public IPage<SysTaskItemVO> findByList(QueryTaskVO queryTaskVO) {
        String idCardNo = queryTaskVO.getIdCardNo();
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_entity_card", idCardNo);
        SysUser sysUser = userService.getBaseMapper().selectOne(queryWrapper);
        Asserts.check(sysUser == null, "身份证 %s 对应的用户不存在！", idCardNo);
        List<DeptDTO> deptList = sysDeptService.selectUserDeptList(sysUser.getId());
        Asserts.check(CollectionUtils.isEmpty(deptList), "身份证 %s 没有绑定部门！", idCardNo);
        DeptDTO deptDTO = deptList.get(0);
        if (Objects.nonNull(deptDTO)) {
            queryTaskVO.setDeptId(deptDTO.getDeptId());
        }
        IPage<SysTaskItemVO> page = new Page<>(queryTaskVO.getPageNumber(), queryTaskVO.getPageSize());
        IPage<SysTaskItemVO> result = sysTaskMapper.findByList(page, queryTaskVO);
        if (CollectionUtils.isNotEmpty(result.getRecords())) {
            List<SysTaskItemVO> records = result.getRecords();
            records.forEach(sysTaskItemVO -> {
                sysTaskItemVO.setRwlxDesc(TaskTypeEnum.getEnum(sysTaskItemVO.getRwlx()).desc);
            });
            getLogs(records);
            result.setRecords(records);
        }
        return result;
    }

    @Override
    public List<SysTaskLogVO> findLogByList(QueryTaskLogVO queryTaskLogVO) {
        LambdaQueryWrapper<SysTaskLog> logQueryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(queryTaskLogVO.getTaskItemId())) {
            logQueryWrapper.eq(SysTaskLog::getTaskItemId, queryTaskLogVO.getTaskItemId());
        }
        if (Objects.nonNull(queryTaskLogVO.getRwId())) {
            logQueryWrapper.eq(SysTaskLog::getRwId, queryTaskLogVO.getRwId());
        }
        if (Objects.nonNull(queryTaskLogVO.getUserId())) {
            logQueryWrapper.eq(SysTaskLog::getRwjsrId, queryTaskLogVO.getUserId());
        }
        List<SysTaskLog> list = sysTaskLogService.list(logQueryWrapper);
        List<SysTaskLogVO> sysTaskLogVOS = BeanUtil.copyToList(list, SysTaskLogVO.class);
        return sysTaskLogVOS;
    }

    @Override
    public Long countByQuery(QueryTaskVO queryTaskVO) {
        String idCardNo = queryTaskVO.getIdCardNo();
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_entity_card", idCardNo);
        queryWrapper.eq("xt_zxbz",DeletedEnum.NATURE.code);
        SysUser sysUser = userService.getBaseMapper().selectOne(queryWrapper);
        Asserts.check(sysUser == null, "身份证 %s 对应的用户不存在！", idCardNo);
        List<DeptDTO> deptList = sysDeptService.selectUserDeptList(sysUser.getId());
        Asserts.check(CollectionUtils.isEmpty(deptList), "身份证 %s 没有绑定部门！", idCardNo);
        DeptDTO deptDTO = deptList.get(0);
        if (Objects.nonNull(deptDTO)) {
            queryTaskVO.setDeptId(deptDTO.getDeptId());
        }
        //查询
        Long count = sysTaskMapper.countByUser(queryTaskVO);
        Long deptCount = sysTaskMapper.countByDept(queryTaskVO);
        return deptCount + count;
    }

    @Override
    public SysTaskCountToday todayCount() {
        long unFinish = this.count(new LambdaQueryWrapper<SysTask>()
                .eq(SysTask::getRwzxzt, ExecuteStatusEnum.UNFINISH.code).between(SysTask::getRwzdsj,
                        LocalDateTimeUtils.todayStart(), LocalDateTimeUtils.todayEnd()));
        long finish = this.count(new LambdaQueryWrapper<SysTask>()
                .eq(SysTask::getRwzxzt, ExecuteStatusEnum.FINISH.code).between(SysTask::getRwzdsj,
                        LocalDateTimeUtils.todayStart(), LocalDateTimeUtils.todayEnd()));
        SysTaskCountToday today = new SysTaskCountToday();
        today.setFinishCount(finish);
        today.setUnFinishCount(unFinish);
        return today;
    }

    @Override
    public SysTaskCountToday allCount() {
        long unFinish = this.count(new LambdaQueryWrapper<SysTask>()
                .eq(SysTask::getRwzxzt, ExecuteStatusEnum.UNFINISH.code));
        long finish = this.count(new LambdaQueryWrapper<SysTask>()
                .eq(SysTask::getRwzxzt, ExecuteStatusEnum.FINISH.code));
        SysTaskCountToday today = new SysTaskCountToday();
        today.setFinishCount(finish);
        today.setUnFinishCount(unFinish);
        return today;
    }

    @Override
    public List<SysTaskCountGroup> groupCount(Integer countType) {
        List<SysTaskCountGroup> groupList = new ArrayList<>();
        initGroupList(groupList);
        if (GroupCountTypeEnum.ALL.code.equals(countType)) {
            List<SysTask> list = this.list(new LambdaQueryWrapper<SysTask>());
            Map<String, List<SysTask>> typeMap = list.stream().collect(Collectors.groupingBy(SysTask::getRwlx));
            groupList.forEach(item -> {
                if (typeMap.containsKey(item.getRwlx())){
                    item.setCount( Long.valueOf(typeMap.get(item.getRwlx()).size()));
                }
            });
        }
        if (GroupCountTypeEnum.TODAY.code.equals(countType)) {
            List<SysTask> list = this.list(new LambdaQueryWrapper<SysTask>().between(SysTask::getRwzdsj,
                    LocalDateTimeUtils.todayStart(), LocalDateTimeUtils.todayEnd()));
            Map<String, List<SysTask>> typeMap = list.stream().collect(Collectors.groupingBy(SysTask::getRwlx));
            groupList.forEach(item -> {
                if (typeMap.containsKey(item.getRwlx())){
                    item.setCount( Long.valueOf(typeMap.get(item.getRwlx()).size()));
                }

            });
        }
        if (GroupCountTypeEnum.WEEK.code.equals(countType)) {
            List<SysTask> list = this.list(new LambdaQueryWrapper<SysTask>().between(SysTask::getRwzdsj,
                    LocalDateTimeUtils.daysStart(7), LocalDateTime.now()));
            Map<String, List<SysTask>> typeMap = list.stream().collect(Collectors.groupingBy(SysTask::getRwlx));
            groupList.forEach(item -> {
                if (typeMap.containsKey(item.getRwlx())){
                    item.setCount( Long.valueOf(typeMap.get(item.getRwlx()).size()));
                }

            });
        }
        if (GroupCountTypeEnum.MONTH.code.equals(countType)) {
            List<SysTask> list = this.list(new LambdaQueryWrapper<SysTask>().between(SysTask::getRwzdsj,
                    LocalDateTimeUtils.daysStart(30), LocalDateTime.now()));
            Map<String, List<SysTask>> typeMap = list.stream().collect(Collectors.groupingBy(SysTask::getRwlx));
            groupList.forEach(item -> {
                if (typeMap.containsKey(item.getRwlx())){
                    item.setCount( Long.valueOf(typeMap.get(item.getRwlx()).size()));
                }

            });
        }
        return groupList;
    }

    private void initGroupList(List<SysTaskCountGroup> groupList) {
        TaskTypeEnum[] values = TaskTypeEnum.values();
        for (TaskTypeEnum value : values) {
            SysTaskCountGroup group = new SysTaskCountGroup();
            group.setCount(0L);
            group.setRwlx(value.code);
            groupList.add(group);
        }

    }

    @Override
    public List<SysTaskLineChartVO> lineChart(Integer lineChartType) {
        if (LineChartTypeEnum.DAY.code.equals(lineChartType)) {
            List<SysTaskLineChartVO> sysTaskLineChartVOS = getDay();
            sysTaskLineChartVOS.sort(Comparator.comparing(SysTaskLineChartVO::getNo));
            return sysTaskLineChartVOS;
        } else {
            List<SysTaskLineChartVO> sysTaskLineChartVOS = getMonth();
            sysTaskLineChartVOS.sort(Comparator.comparing(SysTaskLineChartVO::getNo));
            return sysTaskLineChartVOS;
        }
    }

    private List<SysTaskLineChartVO> getMonth() {
        List<SysTaskLineChartVO> sysTaskLineChartVOS = new ArrayList<>();
        Map<String, Long> monthGroup = getMonthGroup(12);
        //获取最近七天的数据
        LocalDateTime begin = LocalDateTimeUtils.todayStart().minusYears(1);
        LocalDateTime after = LocalDateTimeUtils.todayEnd();
        List<SysTask> list = this.list(new LambdaQueryWrapper<SysTask>().between(SysTask::getRwzdsj, begin, after));
        Map<String, List<SysTask>> taskMap = list.stream().collect(Collectors.groupingBy(item -> DateUtils.parseDateToStr(DateUtils.YY_MM, DateUtils.LocalDateTimeToDate(item.getRwzdsj()))));
        monthGroup.entrySet().forEach(item -> {
            SysTaskLineChartVO chartVO = new SysTaskLineChartVO();
            if (taskMap.containsKey(item.getKey())) {
                List<SysTask> sysTasks = taskMap.get(item.getKey());
                chartVO.setNo(item.getKey());
                chartVO.setCount(Long.valueOf(sysTasks.size()));
            } else {
                chartVO.setNo(item.getKey());
                chartVO.setCount(0L);
            }
            sysTaskLineChartVOS.add(chartVO);
        });
        return sysTaskLineChartVOS;
    }

    private List<SysTaskLineChartVO> getDay() {
        List<SysTaskLineChartVO> sysTaskLineChartVOS = new ArrayList<>();
        //获取最近七天的数据
        Map<String, Long> dayMap = getDayGroup(7);
        LocalDateTime begin = LocalDateTimeUtils.todayStart().minusDays(7);
        LocalDateTime after = LocalDateTimeUtils.todayEnd();
        List<SysTask> list = this.list(new LambdaQueryWrapper<SysTask>().between(SysTask::getRwzdsj, begin, after));
        Map<String, List<SysTask>> taskMap = list.stream().collect(Collectors.groupingBy(item -> DateUtils.parseDateToStr(DateUtils.MM_DD, DateUtils.LocalDateTimeToDate(item.getRwzdsj()))));
        dayMap.entrySet().forEach(item -> {
            SysTaskLineChartVO chartVO = new SysTaskLineChartVO();
            if (taskMap.containsKey(item.getKey())) {
                List<SysTask> sysTasks = taskMap.get(item.getKey());
                chartVO.setNo(item.getKey());
                chartVO.setCount(Long.valueOf(sysTasks.size()));
            } else {
                chartVO.setNo(item.getKey());
                chartVO.setCount(0L);
            }
            sysTaskLineChartVOS.add(chartVO);
        });
        return sysTaskLineChartVOS;

    }

    private Map<String, Long> getDayGroup(Integer dayNum) {
        Map<String, Long> map = new HashMap<>();
        for (int i = 1; i <= dayNum; i++) {
            map.put(DateUtils.parseDateToStr(DateUtils.MM_DD, DateUtils.LocalDateTimeToDate(LocalDateTime.now().minusDays(i))), 0L);
        }
        return map;
    }

    private Map<String, Long> getMonthGroup(Integer monthNum) {
        Map<String, Long> map = new HashMap<>();
        for (int i = 1; i <= monthNum; i++) {
            map.put(DateUtils.parseDateToStr(DateUtils.YY_MM, DateUtils.LocalDateTimeToDate(LocalDateTime.now().minusMonths(i))), 0L);
        }
        return map;

    }


    /**
     * 获取对应任务的日志
     *
     * @param records
     */
    private void getLogs(List<SysTaskItemVO> records) {
        List<Long> taskItemIds = records.stream().map(SysTaskItemVO::getId).collect(Collectors.toList());
        List<SysTaskLog> list = sysTaskLogService.list(new LambdaQueryWrapper<SysTaskLog>()
                .in(SysTaskLog::getTaskItemId, taskItemIds).orderByDesc(SysTaskLog::getRwczsj));
        if (CollectionUtils.isNotEmpty(list)) {
            Map<Long, List<SysTaskLog>> itemMap =
                    list.stream().collect(Collectors.groupingBy(SysTaskLog::getTaskItemId));
            records.forEach(item -> {
                List<SysTaskLog> sysTaskLogs = itemMap.get(item.getId());
                List<SysTaskLogVO> sysTaskLogVOS = BeanUtil.copyToList(sysTaskLogs, SysTaskLogVO.class);
                sysTaskLogVOS.forEach(log -> {
                    log.setRwczztDesc(TaskLogStatusEnum.valueOf(log.getRwczzt()).desc);
                });
                item.setLogs(sysTaskLogVOS);
            });
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveInfo(AddSysTaskVO addSysTaskVO, String ipAddress) {
        //1.记录任务    如果是执行类型部门，
        // 判断规则为单人且下发部门id 不能为空
        //如果是执行类型为 人  那么下发用户List不能为空
        validInfo(addSysTaskVO);
        SysTask sysTask = BeanUtil.copyProperties(addSysTaskVO, SysTask.class);
        sysTask.setXtZhxgsj(LocalDateTime.now());
        sysTask.setXtCjsj(LocalDateTime.now());
        sysTask.setXtLrip(ipAddress);
        sysTask.setXtZhxgid(addSysTaskVO.getRwcjrId());
        sysTask.setXtZhxgrxm(addSysTaskVO.getRwcjrxm());
        sysTask.setXtZhxgrbmid(addSysTaskVO.getRwcjrbmId());
        sysTask.setXtZhxgrbm(addSysTaskVO.getRwcjrbmmc());

        sysTask.setXtLrsj(LocalDateTime.now());
        sysTask.setXtLrrid(addSysTaskVO.getRwcjrId());
        sysTask.setXtLrrxm(addSysTaskVO.getRwcjrxm());
        sysTask.setXtLrrbmid(addSysTaskVO.getRwcjrbmId());
        sysTask.setXtLrrbm(addSysTaskVO.getRwcjrbmmc());
        sysTask.setYyId(addSysTaskVO.getAppId());
        this.save(sysTask);
        //2。下发相关任务
        saveItems(addSysTaskVO, sysTask);
    }

    @Override
    @Transactional
    public SysTask detail(DetailSysTaskVO detailSysTaskVO, String ipAddress) {
        String idCardNo = detailSysTaskVO.getIdCardNo();
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_entity_card", idCardNo);
        SysUser sysUser = userService.getBaseMapper().selectOne(queryWrapper);
        Asserts.check(sysUser == null, "身份证 %s 对应的用户不存在！", idCardNo);
        List<DeptDTO> deptList = sysDeptService.selectUserDeptList(sysUser.getId());
        DeptDTO deptDTO = deptList.get(0);
        //1。更新对应任务下发表为已查看
        SysTaskItem taskItem = sysTaskItemService.getById(detailSysTaskVO.getTaskItemId());
        if (TaskItemStatusEnum.NOTVIEWED.code.equals(taskItem.getRwzt())) {
            taskItem.setRwcksj(LocalDateTime.now());
            taskItem.setRwzt(TaskItemStatusEnum.VIEWED.code);
            taskItem.setXtZxbz(DeletedEnum.NATURE.code);
            taskItem.setXtZhxgsj(LocalDateTime.now());
            taskItem.setXtLrip(ipAddress);
            taskItem.setXtZhxgid(sysUser.getId());
            taskItem.setXtZhxgrxm(sysUser.getUserName());
            taskItem.setXtZhxgrbmid(deptDTO.getDeptId());
            taskItem.setXtZhxgrbm(deptDTO.getDeptName());
            sysTaskItemService.updateById(taskItem);
        }
        //2。插入日志
        SysTaskLog log = new SysTaskLog();
        log.setRwId(taskItem.getRwId());
        log.setRwjsrId(sysUser.getId());
        log.setTaskItemId(taskItem.getId());
        log.setRwczsj(LocalDateTime.now());
        log.setRwczzt(TaskItemStatusEnum.VIEWED.code);
        sysTaskLogService.save(log);
        //返回任务详情
        SysTask task = this.getById(taskItem.getRwId());
        task.setId(detailSysTaskVO.getTaskItemId());
        return task;
    }

    @Override
    @Transactional
    public void finish(FinishSysTaskVO finishSysTaskVO, String ipAddress) {
        List<SysTask> list = this.list(new LambdaQueryWrapper<SysTask>().eq(SysTask::getRwlyId, finishSysTaskVO.getLyxtId()));
        if (CollectionUtils.isEmpty(list)){
            Asserts.check(true, "没有查到对应任务", finishSysTaskVO.getLyxtId());
        }
        SysTask sourceTask = list.get(0);
        //1。更新对应任务下发表为完成
        String idCardNo = finishSysTaskVO.getIdCardNo();
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_entity_card", idCardNo);
        SysUser sysUser = userService.getBaseMapper().selectOne(queryWrapper);
        Asserts.check(sysUser == null, "身份证 %s 对应的用户不存在！", idCardNo);
        List<DeptDTO> deptList = sysDeptService.selectUserDeptList(sysUser.getId());
        DeptDTO deptDTO = deptList.get(0);
        SysTaskItem taskItem =null;
        if (ExecuteTypeEnum.USER.code.equals(sourceTask.getZxlx())){
             taskItem = sysTaskItemService.getOne(new LambdaQueryWrapper<SysTaskItem>()
                    .eq(SysTaskItem::getRwId, sourceTask.getId()).eq(SysTaskItem::getRwjsrsfzh, idCardNo));
        }else{
            Asserts.check(StringUtils.isBlank(finishSysTaskVO.getDeptCode()), "任务类型为部门时部门编码不能为空", finishSysTaskVO.getDeptCode());
            SysDept sysDept = sysDeptService.getOne(new LambdaQueryWrapper<SysDept>()
                    .eq(SysDept::getOrgCode, finishSysTaskVO.getDeptCode())
                    .eq(SysDept::getXtZxbz, DeletedEnum.NATURE.code));
            Asserts.check(sysDept == null, "部门编码 %s 对应的部门不存在！", finishSysTaskVO.getDeptCode());
            taskItem = sysTaskItemService.getOne(new LambdaQueryWrapper<SysTaskItem>()
                    .eq(SysTaskItem::getRwId, sourceTask.getId()).eq(SysTaskItem::getRwjsrbmId,sysDept.getId()));
        }
        Asserts.check(TaskItemStatusEnum.FINISH.code.equals(taskItem.getRwzt()), "该任务已经完成，请勿重复完成");
        taskItem.setRwwcsj(LocalDateTime.now());
        taskItem.setRwzt(TaskItemStatusEnum.FINISH.code);
        taskItem.setXtZhxgsj(LocalDateTime.now());
        taskItem.setXtLrip(ipAddress);
        taskItem.setXtZhxgid(sysUser.getId());
        taskItem.setXtZhxgrxm(sysUser.getUserName());
        taskItem.setXtZhxgrbmid(deptDTO.getDeptId());
        taskItem.setXtZhxgrbm(deptDTO.getDeptName());
        sysTaskItemService.updateById(taskItem);
        //2。插入日志
        SysTaskLog log = new SysTaskLog();
        log.setRwId(taskItem.getRwId());
        log.setRwjsrId(sysUser.getId());
        log.setTaskItemId(taskItem.getId());
        log.setRwczsj(LocalDateTime.now());
        log.setRwczzt(TaskItemStatusEnum.FINISH.code);
        sysTaskLogService.save(log);
        //3 判断任务是否执行完成
        SysTask task = this.getById(taskItem.getRwId());
        if (ExecuteTypeEnum.DEPT.code.equals(task.getZxlx()) || RuleTypeEnum.ONE.code.equals(task.getRwcjrgz())) {
            task.setRwzxzt(2);
            task.setXtZhxgsj(LocalDateTime.now());
            task.setXtLrip(ipAddress);
            task.setXtZhxgid(sysUser.getId());
            task.setXtZhxgrxm(sysUser.getUserName());
            task.setXtZhxgrbmid(deptDTO.getDeptId());
            task.setXtZhxgrbm(deptDTO.getDeptName());
            this.updateById(task);
        } else {
            int count = sysTaskItemService.count(new LambdaQueryWrapper<SysTaskItem>()
                    .ne(SysTaskItem::getId, finishSysTaskVO.getTaskItemId()).ne(SysTaskItem::getRwzt, TaskItemStatusEnum.FINISH.code));
            if (count == 0) {
                task.setRwzxzt(2);
                task.setXtZhxgsj(LocalDateTime.now());
                task.setXtLrip(ipAddress);
                task.setXtZhxgid(sysUser.getId());
                task.setXtZhxgrxm(sysUser.getUserName());
                task.setXtZhxgrbmid(deptDTO.getDeptId());
                task.setXtZhxgrbm(deptDTO.getDeptName());
                this.updateById(task);
            }
        }

    }


    private void saveItems(AddSysTaskVO addSysTaskVO, SysTask sysTask) {
        List<SysTaskLog> logs = new ArrayList<>();
        if (ExecuteTypeEnum.USER.code.equals(addSysTaskVO.getZxlx())) {
            List<AddSysTaskUserInfoVO> userInfos = addSysTaskVO.getUserInfos();
            userInfos.forEach(item -> {
                SysTaskItem taskItem = new SysTaskItem();
                taskItem.setRwId(sysTask.getId());
                QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("id_entity_card", item.getIdCardNo());
                SysUser sysUser = userService.getBaseMapper().selectOne(queryWrapper);
                Asserts.check(sysUser == null, "身份证 %s 对应的用户不存在！", item.getIdCardNo());
                taskItem.setRwjsrId(sysUser.getId());
                List<DeptDTO> deptList = sysDeptService.selectUserDeptList(sysUser.getId());
                DeptDTO deptDTO = deptList.get(0);
                taskItem.setRwjsrbmId(deptDTO.getDeptId());
                taskItem.setRwjsrbmmc(deptDTO.getDeptName());
                taskItem.setRwjsrdw(item.getCompanyName());
                taskItem.setRwjsrdwId(item.getCompanyId());
                taskItem.setRwjsrxm(sysUser.getUserName());
                taskItem.setRwjsrsfzh(sysUser.getIdEntityCard());
                sysTaskItemService.save(taskItem);
                SysTaskLog log = new SysTaskLog();
                log.setRwId(sysTask.getId());
                log.setRwjsrId(sysUser.getId());
                log.setTaskItemId(taskItem.getId());
                log.setRwczsj(LocalDateTime.now());
                log.setRwczzt(TaskItemStatusEnum.NOTVIEWED.code);
                logs.add(log);
            });
        } else {
            List<AddSysTaskDeptVO> deptVOS = addSysTaskVO.getDeptVOS();
            deptVOS.forEach(item -> {
                SysTaskItem taskItem = new SysTaskItem();
                taskItem.setRwId(sysTask.getId());
                SysDept one = sysDeptService.getOne(new LambdaQueryWrapper<SysDept>()
                        .eq(SysDept::getOrgCode, item.getDeptCode())
                        .eq(SysDept::getXtZxbz, DeletedEnum.NATURE.code));
                taskItem.setRwjsrbmId(one.getId());
                taskItem.setRwjsrdw(item.getCompanyName());
                taskItem.setRwjsrdwId(item.getCompanyId());
                sysTaskItemService.save(taskItem);
                SysTaskLog log = new SysTaskLog();
                log.setRwId(sysTask.getId());
                log.setTaskItemId(taskItem.getId());
                log.setRwczsj(LocalDateTime.now());
                log.setRwczzt(TaskItemStatusEnum.NOTVIEWED.code);
                logs.add(log);
            });

        }
        sysTaskLogService.saveBatch(logs);


    }

    private void validInfo(AddSysTaskVO addSysTaskVO) {
        Asserts.check(ExecuteTypeEnum.USER.code.equals(addSysTaskVO.getZxlx())
                && CollectionUtil.isEmpty(addSysTaskVO.getUserInfos()), "执行类型为用户用户信息为不能为空");

        Asserts.check(ExecuteTypeEnum.DEPT.code.equals(addSysTaskVO.getZxlx())
                && CollectionUtil.isEmpty(addSysTaskVO.getDeptVOS()), "执行类型为部门，部门信息为不能为空");
    }
}




