package com.bs.ct.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.bs.cm.domain.CmAttach;
import com.bs.cm.service.ICmAttachService;
import com.bs.common.core.domain.entity.SysDept;
import com.bs.common.core.domain.entity.SysUser;
import com.bs.common.exception.UtilException;
import com.bs.ct.domain.CtTaskFeedback;
import com.bs.ct.domain.CtTaskInfo;
import com.bs.ct.domain.CtTaskTag;
import com.bs.ct.service.ICtTaskFeedbackService;
import com.bs.ct.service.ICtTaskInfoService;
import com.bs.ct.service.ICtTaskTagService;
import com.bs.ct.vo.CtTaskBranchVO;
import com.bs.ct.vo.MyTaskVO;
import com.bs.system.service.ISysDeptService;
import com.bs.system.service.ISysUserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.hutool.core.lang.Validator;
import com.bs.ct.domain.CtTaskBranch;
import com.bs.ct.service.ICtTaskBranchService;
import javax.annotation.Resource;

/**
 * 任务机构信息Controller
 *
 * @author bs
 * @date 2025-02-22
 */
@Api(tags = "任务机构信息")
@RestController
@RequestMapping("/task/branch")
public class CtTaskBranchController extends BaseController {
    @Resource
    private ICtTaskBranchService ctTaskBranchService;
    @Autowired
    private ISysUserService userService;
    @Resource
    private ICtTaskInfoService ctTaskInfoService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ICtTaskFeedbackService ctTaskFeedbackService;
    @Resource
    private ICmAttachService cmAttachService;
    @Resource
    private ICtTaskTagService ctTaskTagService;
    /**
     * 分页查询任务机构信息列表
     */
    @ApiOperation("分页查询任务机构信息列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CtTaskBranch ctTaskBranch) {
        startPage();
        LambdaQueryWrapper<CtTaskBranch> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctTaskBranch);
        List<CtTaskBranch> list = ctTaskBranchService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 分页查询任务机构信息列表
     */
    @ApiOperation("分页查询任务机构信息列表")
    @GetMapping("/pageListByBranch")
    public TableDataInfo pageListByBranch(CtTaskBranch ctTaskBranch) {
        Integer pageNum = ctTaskBranch.getPageNum() != null ? ctTaskBranch.getPageNum() : 1;
        Integer pageSize = ctTaskBranch.getPageSize() != null ? ctTaskBranch.getPageSize() : 10;
        List<Long> ownerDeptIds = deptService.getOwnerDeptIds();
        if (ownerDeptIds.isEmpty()) {
            ownerDeptIds.add(0L);
        }
        Long userId = getUserId();
        LambdaQueryWrapper<CtTaskBranch> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctTaskBranch);
        if ((!ownerDeptIds.isEmpty() && ownerDeptIds.size() > 0) && 1 != userId && "1".equals(ctTaskBranch.getIsTask())) {
            queryWrapper.in(CtTaskBranch::getBranchCode,ownerDeptIds);
        }
        queryWrapper.orderByDesc(CtTaskBranch::getCreateTime);
        String inSqlCondition = "SELECT id FROM ct_task_info WHERE del_flag = '0'";
        queryWrapper.inSql(CtTaskBranch::getTaskId, inSqlCondition);
        List<CtTaskBranch> list = ctTaskBranchService.list(queryWrapper);
        setAll(list);
        //过滤
        List<CtTaskBranch> ctTaskBranches = filterCtTaskBranchList(list,ctTaskBranch);
        Integer start = (pageNum - 1) * pageSize;
        List<CtTaskBranch> result = ctTaskBranches.stream()
                .skip(start)  // 跳过前4条数据
                .limit(pageSize) // 最多取6条数据
                .collect(Collectors.toList());
        setTag(result);
        TableDataInfo data = getDataTable(result);
        data.setTotal(ctTaskBranches.size());
        return data;
    }



    private List<CtTaskBranch> filterCtTaskBranchList(List<CtTaskBranch> list, CtTaskBranch filterParams) {
        List<CtTaskBranch> filteredList = new ArrayList<>(list);
        if (filterParams.getTaskType() != null && !filterParams.getTaskType().isEmpty()) {
            filteredList = filteredList.stream()
                    .filter(item -> filterParams.getTaskType().equals(item.getTaskType()))
                    .collect(Collectors.toList());
        }
        if (filterParams.getTaskTitle() != null && !filterParams.getTaskTitle().isEmpty()) {
            filteredList = filteredList.stream()
                    .filter(item -> item.getTaskTitle() != null && item.getTaskTitle().contains(filterParams.getTaskTitle()))
                    .collect(Collectors.toList());
        }
        if (filterParams.getTaskContent() != null && !filterParams.getTaskContent().isEmpty()) {
            filteredList = filteredList.stream()
                    .filter(item -> item.getTaskContent() != null && item.getTaskContent().contains(filterParams.getTaskContent()))
                    .collect(Collectors.toList());
        }
        if (filterParams.getTaskStatus() != null && !filterParams.getTaskStatus().isEmpty()) {
            filteredList = filteredList.stream()
                    .filter(item -> item.getTaskStatus() != null && item.getTaskStatus().contains(filterParams.getTaskStatus()))
                    .collect(Collectors.toList());
        }
        // 修改为使用 startDate 和 endDate 过滤
        Date paramStartDate = filterParams.getStartDate();
        Date paramEndDate = filterParams.getEndDate();
        if (paramStartDate != null && paramEndDate != null) {
            filteredList = filteredList.stream()
                    .filter(item -> {
                        Date itemStartDate = item.getStartDate();
                        Date itemEndDate = item.getEndDate();
                        return itemStartDate != null && itemEndDate != null
                                &&!itemStartDate.before(paramStartDate)
                                &&!itemEndDate.after(paramEndDate);
                    })
                    .collect(Collectors.toList());
        }
        return filteredList;
    }


    /**
     * 导出任务交办列表
     */
    @ApiOperation("导出任务交办列表")
    @Log(title = "任务交办导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportByAssigned")
    public void exportByAssigned(HttpServletResponse response, CtTaskBranch ctTaskBranch) {
        List<Long> ownerDeptIds = deptService.getOwnerDeptIds();
        if (ownerDeptIds.isEmpty()) {
            ownerDeptIds.add(0L);
        }
        Long userId = getUserId();
        LambdaQueryWrapper<CtTaskBranch> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctTaskBranch);
        if ((!ownerDeptIds.isEmpty() && ownerDeptIds.size() > 0) && 1 != userId && "1".equals(ctTaskBranch.getIsTask())) {
            queryWrapper.in(CtTaskBranch::getBranchCode,ownerDeptIds);
        }
        queryWrapper.orderByDesc(CtTaskBranch::getCreateTime);
        String inSqlCondition = "SELECT id FROM ct_task_info WHERE del_flag = '0'";
        queryWrapper.inSql(CtTaskBranch::getTaskId, inSqlCondition);
        List<CtTaskBranch> list = ctTaskBranchService.list(queryWrapper);
        setAll(list);
        List<CtTaskBranch> ctTaskBranches = filterCtTaskBranchList(list,ctTaskBranch);
        List<CtTaskBranchVO> ctTaskBranchVOs = new ArrayList<>();
        for (CtTaskBranch branch : ctTaskBranches) {
            CtTaskBranchVO ctTaskBranchVO = new CtTaskBranchVO();
            BeanUtils.copyProperties(branch,ctTaskBranchVO);
            ctTaskBranchVOs.add(ctTaskBranchVO);
        }
        ExcelUtil<CtTaskBranchVO> util = new ExcelUtil<CtTaskBranchVO>(CtTaskBranchVO. class);
        util.exportExcel(response, ctTaskBranchVOs, "任务交办数据");
    }

    /**
     * 分页查询任务机构信息列表
     */
    @ApiOperation("分页查询任务机构信息列表")
    @GetMapping("/pageListByUser")
    public TableDataInfo pageListByUser(CtTaskBranch ctTaskBranch) {
        Integer pageNum = ctTaskBranch.getPageNum() != null ? ctTaskBranch.getPageNum() : 1;
        Integer pageSize = ctTaskBranch.getPageSize() != null ? ctTaskBranch.getPageSize() : 10;
        String userId = String.valueOf(getUserId());
        LambdaQueryWrapper<CtTaskBranch> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctTaskBranch);
        queryWrapper.orderByDesc(CtTaskBranch::getCreateTime);
        String inSqlCondition = "SELECT id FROM ct_task_info WHERE del_flag = '0'";
        String inSqlConditionByUserId = "SELECT task_id FROM ct_task_branch WHERE del_flag = '0' and type = '人员' and user_id =" + userId;
        queryWrapper.inSql(CtTaskBranch::getTaskId, inSqlCondition);
        queryWrapper.inSql(CtTaskBranch::getId, inSqlConditionByUserId);
        List<CtTaskBranch> list = ctTaskBranchService.list(queryWrapper);
        setAll(list);
        List<CtTaskBranch> ctTaskBranches = filterCtTaskBranchList(list,ctTaskBranch);
        Integer start = (pageNum - 1) * pageSize;
        List<CtTaskBranch> result = ctTaskBranches.stream()
                .skip(start)  // 跳过前4条数据
                .limit(pageSize) // 最多取6条数据
                .collect(Collectors.toList());
        setTag(result);
        TableDataInfo data = getDataTable(result);
        data.setTotal(ctTaskBranches.size());
        return data;
    }

    private void setTag(List<CtTaskBranch> result) {
        for (CtTaskBranch branch : result) {
            Long taskId = branch.getTaskId();
            List<CtTaskTag> ctTaskTags = ctTaskTagService.list(new LambdaQueryWrapper<CtTaskTag>().eq(CtTaskTag::getTaskId, taskId));
            branch.setCtTaskTags(ctTaskTags);
        }
    }

    /**
     * 导出任务交办列表
     */
    @ApiOperation("导出任务交办列表")
    @Log(title = "任务交办导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportByUser")
    public void exportByUser(HttpServletResponse response, CtTaskBranch ctTaskBranch) {
        String userId = String.valueOf(getUserId());
        LambdaQueryWrapper<CtTaskBranch> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctTaskBranch);
        queryWrapper.orderByDesc(CtTaskBranch::getCreateTime);
        String inSqlCondition = "SELECT id FROM ct_task_info WHERE del_flag = '0'";
        String inSqlConditionByUserId = "SELECT task_id FROM ct_task_branch WHERE del_flag = '0' and type = '人员' and user_id =" + userId;
        queryWrapper.inSql(CtTaskBranch::getTaskId, inSqlCondition);
        queryWrapper.inSql(CtTaskBranch::getId, inSqlConditionByUserId);
        List<CtTaskBranch> list = ctTaskBranchService.list(queryWrapper);
        setAll(list);
        List<CtTaskBranch> ctTaskBranches = filterCtTaskBranchList(list,ctTaskBranch);
        List<MyTaskVO> ctTaskBranchVOs = new ArrayList<>();
        for (CtTaskBranch branch : ctTaskBranches) {
            MyTaskVO ctTaskBranchVO = new MyTaskVO();
            BeanUtils.copyProperties(branch,ctTaskBranchVO);
            ctTaskBranchVOs.add(ctTaskBranchVO);
        }
        ExcelUtil<MyTaskVO> util = new ExcelUtil<MyTaskVO>(MyTaskVO. class);
        util.exportExcel(response, ctTaskBranchVOs, "我的任务");

    }


    private void setAll(List<CtTaskBranch> list) {
        List<SysDept> depts = deptService.selectDeptList(new SysDept());
        Map<Long, String> deptMap = depts.stream()
                .collect(Collectors.toMap(SysDept::getDeptId, SysDept::getDeptName));
        Map<Long, Long> deptMapByParent = depts.stream()
                .collect(Collectors.toMap(SysDept::getDeptId, SysDept::getParentId));
        for (CtTaskBranch taskOtherBranch : list) {
            Long branchCode = Long.valueOf(taskOtherBranch.getBranchCode());
            Long branchCodeParent = deptMapByParent.get(branchCode);
            String branchNameParent = deptMap.get(branchCodeParent);
            taskOtherBranch.setBranchNameParent(branchNameParent);
            CtTaskInfo byId = ctTaskInfoService.getById(taskOtherBranch.getTaskId());
            if (null != byId) {
                taskOtherBranch.setStatus(byId.getStatus());
                taskOtherBranch.setTaskType(byId.getTaskType());
                taskOtherBranch.setTaskTitle(byId.getTaskTitle());
                taskOtherBranch.setTaskContent(byId.getTaskContent());
                taskOtherBranch.setTaskDate(byId.getTaskDate());
                taskOtherBranch.setSdTaskOther(byId);
            }
            List<CtTaskFeedback> feedbackListByCreateTime = ctTaskFeedbackService.list(new LambdaQueryWrapper<CtTaskFeedback>()
                    .eq(CtTaskFeedback::getTaskBranchId, taskOtherBranch.getId())
                    .ne(CtTaskFeedback::getStatus, 0)
                    .orderByDesc(CtTaskFeedback::getCreateTime));
            // 如果根据创建时间查询到的反馈列表不为空且至少有一个元素
            if (!feedbackListByCreateTime.isEmpty() && feedbackListByCreateTime.size() > 0) {
                // 将该列表中的第一个元素（因为是倒序排列，所以是最新的一条）设置到当前 SdTaskOtherBranch 对象中
                taskOtherBranch.setSdTaskOtherFeedbackByCreate(feedbackListByCreateTime.get(0));
            }
            List<CtTaskFeedback> feedbackListByFeedbackTime = ctTaskFeedbackService.list(new LambdaQueryWrapper<CtTaskFeedback>()
                    .eq(CtTaskFeedback::getTaskId, taskOtherBranch.getId())
                    .eq(CtTaskFeedback::getStatus, 1)
                    .orderByDesc(CtTaskFeedback::getFeedbackTime));
            // 如果根据反馈时间查询到的反馈列表不为空且至少有一个元素
            if (!feedbackListByFeedbackTime.isEmpty() && feedbackListByFeedbackTime.size() > 0) {
                // 将该列表中的第一个元素（因为是倒序排列，所以是最新的一条）设置到当前 SdTaskOtherBranch 对象中
                taskOtherBranch.setSdTaskOtherFeedbackByFeedback(feedbackListByFeedbackTime.get(0));
            }
        }
        for (CtTaskBranch taskInfo : list) {
            if (null != taskInfo.getSdTaskOther()) {
                CtTaskInfo sdTaskOther = taskInfo.getSdTaskOther();
                List<CmAttach> cmAttachList = cmAttachService.list(new LambdaQueryWrapper<CmAttach>().eq(CmAttach::getFileId,sdTaskOther.getFileId()));
                taskInfo.setTaskInitiator(sdTaskOther.getTaskInitiator());
                sdTaskOther.setFile(cmAttachList);
                Date startDate = sdTaskOther.getStartDate();
                Date endDate = sdTaskOther.getEndDate();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                // 转换日期为字符串
                if (null != startDate && endDate != endDate) {
                    String startDateStr = sdf.format(startDate);
                    String endDateStr = sdf.format(endDate);
                    taskInfo.setStartDate(startDate);
                    taskInfo.setEndDate(endDate);
                    taskInfo.setStartToEndDate(startDateStr + "-" + endDateStr);
                }
                String deptName = deptMap.get(sdTaskOther.getDeptId());
                taskInfo.setDeptName(deptName);
                if ("1".equals(taskInfo.getStatus()) || "0".equals(taskInfo.getStatus())) {
                    //未完成0
                    taskInfo.setTaskStatus("0");
                    // 获取当前日期
                    Date currentDate = new Date();
                    if (endDate.compareTo(currentDate) < 0) {
                        //已逾期1
                        taskInfo.setTaskStatus("1");
                    }
                } else if ("2".equals(taskInfo.getStatus())) {
                    //已完成
                    taskInfo.setTaskStatus("2");
                }
            }
            List<CtTaskBranch> ctTaskBranches = ctTaskBranchService.list(new LambdaQueryWrapper<CtTaskBranch>()
                    .eq(CtTaskBranch::getTaskId,taskInfo.getId())
                    .eq(CtTaskBranch::getType,"人员"));
            String transactDeptNames = "";
            String transactInitiator = "";
            String transactBranchNames = "";
            if (null != ctTaskBranches) {
                List<String> deptNameList = new ArrayList<>();
                List<String> parentDeptNameList = new ArrayList<>();
                List<String> userNameList = new ArrayList<>();
                List<String> userIdList = new ArrayList<>();
                for (CtTaskBranch ctTaskBranchesItem : ctTaskBranches) {
                    String deptNameVo = deptMap.get(Long.valueOf(ctTaskBranchesItem.getBranchCode()));
                    Long parentTaskId = deptMapByParent.get(Long.valueOf(ctTaskBranchesItem.getBranchCode()));
                    String parentDeptName = deptMap.get(parentTaskId);
                    String userName = ctTaskBranchesItem.getUserName();
                    String userId = ctTaskBranchesItem.getUserId();
                    if (deptNameVo != null) {
                        deptNameList.add(deptNameVo);
                    }
                    if (parentDeptName != null) {
                        parentDeptNameList.add(parentDeptName);
                    }
                    if (userName != null) {
                        userNameList.add(userName);
                    }
                    if (null != userId) {
                        userIdList.add(userId);
                    }
                }
                transactDeptNames = String.join(",", deptNameList);
                transactInitiator = String.join(",", userNameList);
                transactBranchNames =  String.join(",", parentDeptNameList);
                taskInfo.setTransactDeptName(transactDeptNames);
                taskInfo.setTransactInitiator(transactInitiator);
                taskInfo.setTransactBranchName(transactBranchNames);
                taskInfo.setUserIds(userIdList);
            }
        }
    }

    /**
     * 查询任务机构信息列表
     */
    @ApiOperation("查询任务机构信息列表")
    @GetMapping("/list")
    public AjaxResult list(CtTaskBranch ctTaskBranch) {
        LambdaQueryWrapper<CtTaskBranch> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctTaskBranch);
        List<CtTaskBranch> list = ctTaskBranchService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出任务机构信息列表
     */
    @ApiOperation("导出任务机构信息列表")
    @Log(title = "任务机构信息导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CtTaskBranch ctTaskBranch) {
        LambdaQueryWrapper<CtTaskBranch> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctTaskBranch);
        List<CtTaskBranch> list = ctTaskBranchService.list(queryWrapper);
        ExcelUtil<CtTaskBranch> util = new ExcelUtil<CtTaskBranch>(CtTaskBranch. class);
        util.exportExcel(response, list, "任务机构信息数据");
    }

    /**
     * 获取任务机构信息详细信息
     */
    @ApiOperation("获取任务机构信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ctTaskBranchService.getById(id));
    }

    /**
     * 新增任务机构信息
     */
    @ApiOperation("新增任务机构信息")
    @Log(title = "任务机构信息新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CtTaskBranch ctTaskBranch) {
        return toAjax(ctTaskBranchService.save(ctTaskBranch));
    }

    /**
     * 指派任务
     */
    @ApiOperation("指派任务")
    @Log(title = "指派任务", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addByUser")
    public AjaxResult addByUser(@RequestBody CtTaskBranch ctTaskBranch) {
        List<CtTaskBranch> ctTaskBranchList = new ArrayList<>();
        List<String> userIds = ctTaskBranch.getUserIds();
        List<Long> taskIds = ctTaskBranch.getTaskIds();
        for (Long taskId :taskIds) {
            for (String userId : userIds) {
                if (isTaskUserCombinationExists(taskId, userId)) {
                    continue; // 如果存在，跳过当前组合
                }
                CtTaskBranch taskBranch = new CtTaskBranch();
                SysUser sysUser = userService.selectUserById(Long.valueOf(userId));
                taskBranch.setBranchCode(String.valueOf(sysUser.getDeptId()));
                taskBranch.setUserName(sysUser.getUserName());
                taskBranch.setUserId(userId);
                taskBranch.setTaskStatus("0");
                taskBranch.setTaskId(taskId);
                taskBranch.setType("人员");
                ctTaskBranchList.add(taskBranch);
            }
        }
        if (null != ctTaskBranchList && ctTaskBranchList.size() > 0) {
            ctTaskBranchService.saveBatch(ctTaskBranchList);
        }
        return toAjax(true);
    }

    /**
     * 撤回任务
     */
    @ApiOperation("撤回任务")
    @Log(title = "撤回任务", businessType = BusinessType.INSERT)
    @PostMapping(value = "/deleteByUser")
    public AjaxResult deleteByUser(@RequestBody CtTaskBranch ctTaskBranch) {
        List<Long> taskIds = ctTaskBranch.getTaskIds();
        ctTaskBranchService.remove(new LambdaQueryWrapper<CtTaskBranch>().in(CtTaskBranch::getTaskId,taskIds));
        return toAjax(true);
    }


    private boolean isTaskUserCombinationExists(Long taskId, String userId) {
        CtTaskBranch branchServiceById = ctTaskBranchService.getById(taskId);
        if (null != branchServiceById) {
            Long taskIdVo = branchServiceById.getTaskId();
            CtTaskInfo ctTaskInfoServiceById = ctTaskInfoService.getById(taskIdVo);
            if (null != ctTaskInfoServiceById) {
                String status = ctTaskInfoServiceById.getStatus();
                if ("0".equals(status)) {
                    throw new UtilException("该任务还未发布");
                }
            }
        }
        List<CtTaskBranch> list = ctTaskBranchService.list(new LambdaQueryWrapper<CtTaskBranch>()
                .eq(CtTaskBranch::getTaskId, taskId)
                .eq(CtTaskBranch::getType,"人员")
                .eq(CtTaskBranch::getUserId, userId));
        if (null != list && list.size() > 0) {
            return true;
        }
        return false;
    }


    /**
     * 修改任务机构信息
     */
    @ApiOperation("修改任务机构信息")
    @Log(title = "任务机构信息修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CtTaskBranch ctTaskBranch) {
        return toAjax(ctTaskBranchService.updateById(ctTaskBranch));
    }

    /**
     * 删除任务机构信息
     */
    @ApiOperation("删除任务机构信息")
    @Log(title = "任务机构信息删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(ctTaskBranchService.removeBatchByIds(ids));
    }

    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<CtTaskBranch> queryWrapper,CtTaskBranch ctTaskBranch){


        //id
        if(Validator.isNotEmpty(ctTaskBranch.getId())){
            queryWrapper.eq(CtTaskBranch::getId,ctTaskBranch.getId());
        }

        //任务id
        if(Validator.isNotEmpty(ctTaskBranch.getTaskId())){
            queryWrapper.eq(CtTaskBranch::getTaskId,ctTaskBranch.getTaskId());
        }

        //类型人员、机构
        if(Validator.isNotEmpty(ctTaskBranch.getType())){
            queryWrapper.eq(CtTaskBranch::getType,ctTaskBranch.getType());
        }

        //机构代码
        if(Validator.isNotEmpty(ctTaskBranch.getBranchCode())){
            queryWrapper.eq(CtTaskBranch::getBranchCode,ctTaskBranch.getBranchCode());
        }

        //机构名称
        if(Validator.isNotEmpty(ctTaskBranch.getBranchName())){
            queryWrapper.eq(CtTaskBranch::getBranchName,ctTaskBranch.getBranchName());
        }

        //任务状态
//        if(Validator.isNotEmpty(ctTaskBranch.getTaskStatus())){
//            queryWrapper.eq(CtTaskBranch::getTaskStatus,ctTaskBranch.getTaskStatus());
//        }

        //备注
        if(Validator.isNotEmpty(ctTaskBranch.getRemarks())){
            queryWrapper.eq(CtTaskBranch::getRemarks,ctTaskBranch.getRemarks());
        }

        //删除标志（0代表存在 2代表删除）
        if(Validator.isNotEmpty(ctTaskBranch.getDelFlag())){
            queryWrapper.eq(CtTaskBranch::getDelFlag,ctTaskBranch.getDelFlag());
        }

        //创建部门
        if(Validator.isNotEmpty(ctTaskBranch.getCreateDept())){
            queryWrapper.eq(CtTaskBranch::getCreateDept,ctTaskBranch.getCreateDept());
        }

        //创建人
        if(Validator.isNotEmpty(ctTaskBranch.getCreateBy())){
            queryWrapper.eq(CtTaskBranch::getCreateBy,ctTaskBranch.getCreateBy());
        }

        //创建时间
        if(Validator.isNotEmpty(ctTaskBranch.getCreateTime())){
            queryWrapper.eq(CtTaskBranch::getCreateTime,ctTaskBranch.getCreateTime());
        }

        //修改人
        if(Validator.isNotEmpty(ctTaskBranch.getUpdateBy())){
            queryWrapper.eq(CtTaskBranch::getUpdateBy,ctTaskBranch.getUpdateBy());
        }

        //修改时间
        if(Validator.isNotEmpty(ctTaskBranch.getUpdateTime())){
            queryWrapper.eq(CtTaskBranch::getUpdateTime,ctTaskBranch.getUpdateTime());
        }

        //租户ID
        if(Validator.isNotEmpty(ctTaskBranch.getTenantId())){
            queryWrapper.eq(CtTaskBranch::getTenantId,ctTaskBranch.getTenantId());
        }

        }

}
