package com.bs.ct.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;

import com.bs.cm.domain.CmAttach;
import com.bs.cm.service.ICmAttachService;
import com.bs.common.core.domain.entity.SysUser;
import com.bs.ct.domain.CtTaskInfo;
import com.bs.ct.service.ICtTaskInfoService;
import com.bs.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.CtTaskFeedback;
import com.bs.ct.service.ICtTaskFeedbackService;
import javax.annotation.Resource;

/**
 * 任务反馈Controller
 *
 * @author bs
 * @date 2025-02-22
 */
@Api(tags = "任务反馈")
@RestController
@RequestMapping("/task/feedback")
public class CtTaskFeedbackController extends BaseController {
    @Resource
    private ICtTaskFeedbackService ctTaskFeedbackService;
    @Resource
    private ICtTaskInfoService ctTaskInfoService;
    @Autowired
    private ISysUserService userService;
    @Resource
    private ICmAttachService cmAttachService;
    /**
     * 分页查询任务反馈列表
     */
    @ApiOperation("分页查询任务反馈列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CtTaskFeedback ctTaskFeedback) {
        startPage();
        LambdaQueryWrapper<CtTaskFeedback> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctTaskFeedback);
        List<CtTaskFeedback> list = ctTaskFeedbackService.list(queryWrapper);
        return getDataTable(list);
    }

    private void setFile(List<CtTaskFeedback> list) {
        for (CtTaskFeedback ctFeedback : list) {
            List<CmAttach> cmAttachList = cmAttachService.list(new LambdaQueryWrapper<CmAttach>().eq(CmAttach::getFileId,ctFeedback.getFileId()));
            ctFeedback.setFile(cmAttachList);
        }
    }

    /**
     * 查询任务反馈列表
     */
    @ApiOperation("查询任务反馈列表")
    @GetMapping("/list")
    public AjaxResult list(CtTaskFeedback ctTaskFeedback) {
        LambdaQueryWrapper<CtTaskFeedback> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctTaskFeedback);
        List<CtTaskFeedback> list = ctTaskFeedbackService.list(queryWrapper);
        setFile(list);
        return success(list);
    }

    /**
     * 导出任务反馈列表
     */
    @ApiOperation("导出任务反馈列表")
    @Log(title = "任务反馈导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CtTaskFeedback ctTaskFeedback) {
        LambdaQueryWrapper<CtTaskFeedback> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctTaskFeedback);
        List<CtTaskFeedback> list = ctTaskFeedbackService.list(queryWrapper);
        ExcelUtil<CtTaskFeedback> util = new ExcelUtil<CtTaskFeedback>(CtTaskFeedback. class);
        util.exportExcel(response, list, "任务反馈数据");
    }

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

    /**
     * 新增任务反馈
     */
    @ApiOperation("新增任务反馈")
    @Log(title = "任务反馈新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CtTaskFeedback ctTaskFeedback) {
        updateFile(ctTaskFeedback);
        return toAjax(ctTaskFeedbackService.save(ctTaskFeedback));
    }

    private void updateFile(CtTaskFeedback ctTaskFeedback) {
        Long newId = System.currentTimeMillis() + new Random().nextInt(1000);
        List<CmAttach> file = ctTaskFeedback.getFile();
        if (null != file) {
            for (CmAttach cmAttach : file) {
                cmAttach.setFileId(String.valueOf(newId));
            }
            cmAttachService.updateBatchById(file);
            ctTaskFeedback.setFileId(newId);
        }
    }

    /**
     * 修改任务反馈
     */
    @ApiOperation("修改任务反馈")
    @Log(title = "任务反馈修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CtTaskFeedback ctTaskFeedback) {
        updateFile(ctTaskFeedback);
        return toAjax(ctTaskFeedbackService.updateById(ctTaskFeedback));
    }

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

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


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

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

        //机构任务id
        if(Validator.isNotEmpty(ctTaskFeedback.getTaskBranchId())){
            queryWrapper.eq(CtTaskFeedback::getTaskBranchId,ctTaskFeedback.getTaskBranchId());
        }

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

        //任务反馈时间
        if(Validator.isNotEmpty(ctTaskFeedback.getFeedbackTime())){
            queryWrapper.eq(CtTaskFeedback::getFeedbackTime,ctTaskFeedback.getFeedbackTime());
        }

        //任务完成情况
        if(Validator.isNotEmpty(ctTaskFeedback.getTaskDesc())){
            queryWrapper.eq(CtTaskFeedback::getTaskDesc,ctTaskFeedback.getTaskDesc());
        }

        //操作人员工号
        if(Validator.isNotEmpty(ctTaskFeedback.getUserId())){
            queryWrapper.eq(CtTaskFeedback::getUserId,ctTaskFeedback.getUserId());
        }

        //操作人员姓名
        if(Validator.isNotEmpty(ctTaskFeedback.getUserName())){
            queryWrapper.eq(CtTaskFeedback::getUserName,ctTaskFeedback.getUserName());
        }

        //操作人员部门
        if(Validator.isNotEmpty(ctTaskFeedback.getUserDept())){
            queryWrapper.eq(CtTaskFeedback::getUserDept,ctTaskFeedback.getUserDept());
        }

        //是否已拍照留存
        if(Validator.isNotEmpty(ctTaskFeedback.getIsPhoto())){
            queryWrapper.eq(CtTaskFeedback::getIsPhoto,ctTaskFeedback.getIsPhoto());
        }

        //附件id
        if(Validator.isNotEmpty(ctTaskFeedback.getFileId())){
            queryWrapper.eq(CtTaskFeedback::getFileId,ctTaskFeedback.getFileId());
        }

        //审核人员id
        if(Validator.isNotEmpty(ctTaskFeedback.getCheckUserId())){
            queryWrapper.eq(CtTaskFeedback::getCheckUserId,ctTaskFeedback.getCheckUserId());
        }

        //审核人员姓名
        if(Validator.isNotEmpty(ctTaskFeedback.getCheckUserName())){
            queryWrapper.eq(CtTaskFeedback::getCheckUserName,ctTaskFeedback.getCheckUserName());
        }

        //审核人员部门
        if(Validator.isNotEmpty(ctTaskFeedback.getCheckUserDept())){
            queryWrapper.eq(CtTaskFeedback::getCheckUserDept,ctTaskFeedback.getCheckUserDept());
        }

        //审核时间
        if(Validator.isNotEmpty(ctTaskFeedback.getCheckDate())){
            queryWrapper.eq(CtTaskFeedback::getCheckDate,ctTaskFeedback.getCheckDate());
        }

        //审核状态
        if(Validator.isNotEmpty(ctTaskFeedback.getStatus())){
            queryWrapper.eq(CtTaskFeedback::getStatus,ctTaskFeedback.getStatus());
        }

        //审核信息
        if(Validator.isNotEmpty(ctTaskFeedback.getCheckInfo())){
            queryWrapper.eq(CtTaskFeedback::getCheckInfo,ctTaskFeedback.getCheckInfo());
        }

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

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

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

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

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

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

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

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

        }

}
