package org.jeecg.modules.stm.rate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.jeecg.modules.stm.rate.entity.RateTask;
import org.jeecg.modules.stm.rate.entity.RateTaskdept;
import org.jeecg.modules.stm.rate.entity.RateTaskuser;
import org.jeecg.modules.stm.rate.service.IRateTaskService;
import org.jeecg.modules.stm.rate.service.IRateTaskdeptService;
import org.jeecg.modules.stm.rate.service.IRateTaskuserService;
import org.jeecg.modules.stm.rate.vo.TaskDeptDomainVo;
import org.apache.shiro.SecurityUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


/**
 * @Description: 任务分配
 * @Author: jeecg-boot
 * @Date:   2023-09-23
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/rate/rateTaskdept")
@Slf4j
public class RateTaskdeptController extends JeecgController<RateTaskdept, IRateTaskdeptService> {
	@Autowired
	private IRateTaskdeptService rateTaskdeptService;
	@Autowired
	public IRateTaskService rateTaskService;
	@Autowired
	private IRateTaskuserService rateTaskuserService;
	/**
	 * 分页列表查询
	 *
	 * @param rateTaskdept
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "任务分配-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<RateTaskdept>> queryPageList(RateTaskdept rateTaskdept,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String userid=sysUser.getId();
		String username=sysUser.getUsername();
		QueryWrapper<RateTaskdept> queryWrapper = new QueryWrapper();//QueryGenerator.initQueryWrapper(rateTaskdept, req.getParameterMap());
		Page<RateTaskdept> page = new Page<RateTaskdept>(pageNo, pageSize);
		//IPage<RateTaskdept> pageList = rateTaskdeptService.page(page, queryWrapper);
		queryWrapper.eq("rate_task_id",rateTaskdept.getRateTaskId());
		queryWrapper.apply("(rate_user='"+userid+"' or option_user='"+username+"')");
		if(rateTaskdept.getRatedomain()!=null){
			queryWrapper.eq("ratedomain",rateTaskdept.getRatedomain());
		}
		if(rateTaskdept.getSondomain()!=null&&!rateTaskdept.getSondomain().equals("")){
			//queryWrapper.eq("sondomain",rateTaskdept.getSondomain());
			//queryWrapper.like("sondomain",rateTaskdept.getSondomain());
			queryWrapper.apply("(sondomain like '%"+rateTaskdept.getSondomain()+"%')");
			//queryWrapper.like("sondomain",rateTaskdept.getSondomain());
		}
		//queryWrapper.eq("rate_user",userid);
		//queryWrapper.or();
		//queryWrapper.eq("option_user",username);
		IPage<RateTaskdept> pageList = rateTaskdeptService.pagePrepar(page, queryWrapper);

		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param rateTaskdept
	 * @return
	 */
	@AutoLog(value = "任务分配-添加")
	  
	//@RequiresPermissions("rate:rate_taskdept:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RateTaskdept rateTaskdept) {
		rateTaskdeptService.save(rateTaskdept);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param rateTaskdept
	 * @return
	 */
	@AutoLog(value = "任务分配-编辑")
	  
	//@RequiresPermissions("rate:rate_taskdept:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RateTaskdept rateTaskdept) {

		//rateTaskdeptService.updateById(rateTaskdept);
		rateTaskdeptService.updateTaskDeptInitById(rateTaskdept);

		RateTask rateTask=new RateTask();
		rateTask.setId(rateTaskdept.getRateTaskId());

		//查询 rate_taskdept 看是否都分配完毕，
		QueryWrapper<RateTaskdept> chdomainQueryWrapper  = new QueryWrapper<>();
		chdomainQueryWrapper.eq("rate_task_id",rateTaskdept.getRateTaskId());
		chdomainQueryWrapper.isNull("rate_user");
		List<RateTaskdept> chdomainlist = rateTaskdeptService.list(chdomainQueryWrapper);
		if(chdomainlist!=null&&chdomainlist.size()>0){
			//分配中
			rateTask.setRateTaskstatus("2") ;//分配中
		}else{
			rateTask.setRateTaskstatus("3") ;//已分配
			//分配完成后，给taskuser 的 deptid 分配值。即给 同一个任务id 同一个rate_domain_id 能产生同一个 deptid, 也有部门负责人，赋值给taskuser的- 操作人 和部门id
			QueryWrapper<RateTaskdept> deptQueryWrapper  = new QueryWrapper<>();
			deptQueryWrapper.eq("rate_task_id",rateTaskdept.getRateTaskId());
			List<RateTaskdept> deptlist = rateTaskdeptService.list(deptQueryWrapper);
			for(int i=0;i<deptlist.size();i++){
				RateTaskdept dept=deptlist.get(i);

				QueryWrapper<RateTaskuser> userQueryWrapper  = new QueryWrapper<>();
				userQueryWrapper.eq("rate_task_id",dept.getRateTaskId());
				userQueryWrapper.eq("rate_domain_id",dept.getRateDomainId());
				List<RateTaskuser> userlist = rateTaskuserService.list(userQueryWrapper);
				for(int j=0;j<userlist.size();j++){
					RateTaskuser user=userlist.get(j);
					RateTaskuser userben=new RateTaskuser();
					userben.setId(user.getId());
					userben.setTaskdeptId(dept.getId());
					userben.setOptionUser(dept.getRateUser());
					rateTaskuserService.updateById(userben);

				}



			}



		}
		rateTaskService.updateById(rateTask);


		return Result.OK("任务分配成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "任务分配-通过id删除")
	  
	//@RequiresPermissions("rate:rate_taskdept:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		rateTaskdeptService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "任务分配-批量删除")
	  
	//@RequiresPermissions("rate:rate_taskdept:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.rateTaskdeptService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "任务分配-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<RateTaskdept> queryById(@RequestParam(name="id",required=true) String id) {
		RateTaskdept rateTaskdept = rateTaskdeptService.getById(id);
		if(rateTaskdept==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(rateTaskdept);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param rateTaskdept
    */
   // @RequiresPermissions("rate:rate_taskdept:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RateTaskdept rateTaskdept) {
        return super.exportXls(request, rateTaskdept, RateTaskdept.class, "任务分配");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
   // @RequiresPermissions("rate:rate_taskdept:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RateTaskdept.class);
    }

	   
	 @RequestMapping(value = "/taskDeptDomainTreeList", method = RequestMethod.GET)
	 public Result<TaskDeptDomainVo> taskDeptDomainTreeList(String taskid){

		 TaskDeptDomainVo taskDeptDomainVo = rateTaskdeptService.taskDeptDomainTreeListByTaskid(taskid);
		 return Result.OK(taskDeptDomainVo);
	 }

	  
	@RequestMapping(value = "/taskDeptDomainTreeListByid", method = RequestMethod.GET)
	public Result<TaskDeptDomainVo> taskDeptDomainTreeListByid(String id){

		TaskDeptDomainVo taskDeptDomainVo = rateTaskdeptService.taskDeptDomainTreeListByid(id);
		return Result.OK(taskDeptDomainVo);
	}

	  
	@RequestMapping(value = "/taskUserDeptDomainTreeList", method = RequestMethod.GET)
	public Result<TaskDeptDomainVo> taskUserDeptDomainTreeList(String rateTaskId){
    //why mod

		//TaskDeptDomainVo taskDeptDomainVo = rateTaskdeptService.taskUserDeptDomainTreeListByTaskDeptid(taskdeptId);
		TaskDeptDomainVo taskDeptDomainVo = rateTaskdeptService.taskUserDeptDomainTreeListByTaskId(rateTaskId);
		return Result.OK(taskDeptDomainVo);
	}

	  
	@RequestMapping(value = "/taskUserDeptDomainTreeListByid", method = RequestMethod.GET)
	public Result<TaskDeptDomainVo> taskUserDeptDomainTreeListByid(String id){

		TaskDeptDomainVo taskDeptDomainVo = rateTaskdeptService.taskUserDeptDomainTreeListByid(id);
		return Result.OK(taskDeptDomainVo);
	}


}
