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

import java.util.Arrays;
import java.util.List;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserDepartService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.stm.rate.entity.RateTaskuser;
import org.jeecg.modules.stm.rate.service.IRateTaskuserService;

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.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
 
 
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 任务分配
 * @Author: jeecg-boot
 * @Date:   2023-10-09
 * @Version: V1.0
 */

@RestController
@RequestMapping("/rate/rateTaskuser")
@Slf4j
public class RateTaskuserController extends JeecgController<RateTaskuser, IRateTaskuserService> {
	@Autowired
	private IRateTaskuserService rateTaskuserService;
	 @Autowired
	 private ISysDepartService sysDepartService;
	 @Autowired
	 private ISysUserService sysUserService;
	 @Autowired
	 private ISysUserDepartService sysUserDepartService;
	
	/**
	 * 分页列表查询
	 *
	 * @param rateTaskuser
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "任务分配-分页列表查询")

	@GetMapping(value = "/list")
	public Result<IPage<RateTaskuser>> queryPageList(RateTaskuser rateTaskuser,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String username=sysUser.getUsername();
		String userid=sysUser.getId();
		QueryWrapper<RateTaskuser> queryWrapper = new QueryWrapper();//QueryGenerator.initQueryWrapper(rateTaskuser, req.getParameterMap());
		Page<RateTaskuser> page = new Page<RateTaskuser>(pageNo, pageSize);
		queryWrapper.eq("option_user",userid);
		queryWrapper.eq("rate_task_id",rateTaskuser.getRateTaskId());
		queryWrapper.orderByAsc("valindex");
		queryWrapper.orderByAsc("keyindex");

		if(rateTaskuser.getSondomain()!=null&&!rateTaskuser.getSondomain().equals("")){

			queryWrapper.apply("(sondomain like '%"+rateTaskuser.getSondomain()+"%')");

		}
		if(rateTaskuser.getKeyitem()!=null&&!rateTaskuser.getKeyitem().equals("")){

			queryWrapper.apply("(keyitem like '%"+rateTaskuser.getKeyitem()+"%')");

		}


		IPage<RateTaskuser> pageList = rateTaskuserService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param rateTaskuser
	 * @return
	 */
	@AutoLog(value = "任务分配-添加")

	//@RequiresPermissions("rate:rate_taskuser:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RateTaskuser rateTaskuser) {
		rateTaskuserService.save(rateTaskuser);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param rateTaskuser
	 * @return
	 */
	@AutoLog(value = "任务分配-编辑")

	//@RequiresPermissions("rate:rate_taskuser:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RateTaskuser rateTaskuser) {
		System.out.println("-----------------why ok");

		String keyitemary[]=rateTaskuser.getKeyitemId().replaceAll("\"","").split(",");
		for(int i=0;i<keyitemary.length;i++){
			QueryWrapper<RateTaskuser> rateDomainQueryWrapper = new QueryWrapper<>();
			//rateDomainQueryWrapper.eq("taskdept_id",rateTaskuser.getTaskdeptId());
			rateDomainQueryWrapper.eq("rate_task_id",rateTaskuser.getRateTaskId());
			rateDomainQueryWrapper.eq("keyitem_id",keyitemary[i]);

			List<RateTaskuser> domainlist = rateTaskuserService.list(rateDomainQueryWrapper);
			if(domainlist.size()>0){
				RateTaskuser taskuser=domainlist.get(0);
				RateTaskuser taskuserbea=new RateTaskuser();
				taskuserbea.setId(taskuser.getId());
				taskuserbea.setRateUser(rateTaskuser.getRateUser());
				//根据用户得到部门 code
				SysUser user=sysUserService.getById(rateTaskuser.getRateUser());
				taskuserbea.setRateDept(user.getOrgCode());

				//taskuserbea.setOptionTime(new Date());
				rateTaskuserService.updateById(taskuserbea);

			}

		}




		//rateTaskuserService.updateById(rateTaskuser);
		//有 taskdeptid keyitem id 就可以给赋值了

		return Result.OK("分配填报人成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "任务分配-通过id删除")

	//@RequiresPermissions("rate:rate_taskuser:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		rateTaskuserService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "任务分配-批量删除")

	//@RequiresPermissions("rate:rate_taskuser:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.rateTaskuserService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "任务分配-通过id查询")

	@GetMapping(value = "/queryById")
	public Result<RateTaskuser> queryById(@RequestParam(name="id",required=true) String id) {
		RateTaskuser rateTaskuser = rateTaskuserService.getById(id);
		if(rateTaskuser==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(rateTaskuser);
	}

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

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

}
