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

import java.util.Arrays;
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.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.stm.rate.entity.RateTaskdetail;
import org.jeecg.modules.stm.rate.service.IRateTaskdetailService;

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-09-26
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/rate/rateTaskdetail")
@Slf4j
public class RateTaskdetailController extends JeecgController<RateTaskdetail, IRateTaskdetailService> {
	@Autowired
	private IRateTaskdetailService rateTaskdetailService;
	
	/**
	 * 分页列表查询
	 *
	 * @param rateTaskdetail
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "评估表填报-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<RateTaskdetail>> queryPageList(RateTaskdetail rateTaskdetail,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String appusername=sysUser.getUsername();
		String dutyuserid=sysUser.getId();
		QueryWrapper<RateTaskdetail> queryWrapper = QueryGenerator.initQueryWrapper(rateTaskdetail, req.getParameterMap());
		Page<RateTaskdetail> page = new Page<RateTaskdetail>(pageNo, pageSize);

		queryWrapper.eq("duty_user",dutyuserid);
		queryWrapper.apply(" rate_taskstatus in('4','5','6','9','10','12','13','11','7','8') or (approveuser='"+dutyuserid+"' and rate_approve='1' and rate_taskstatus ='7')");
		//queryWrapper.or();
		//queryWrapper.eq("approveuser",appusername);

		IPage<RateTaskdetail> pageList = rateTaskdetailService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加rateStdTreeList
	 *
	 * @param rateTaskdetail
	 * @return
	 */
	@AutoLog(value = "评估表填报-添加")
	  
	//@RequiresPermissions("rate:rate_taskdetail:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RateTaskdetail rateTaskdetail) {
		rateTaskdetailService.save(rateTaskdetail);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param rateTaskdetail
	 * @return
	 */
	@AutoLog(value = "评估表填报-编辑")
	  
	//@RequiresPermissions("rate:rate_taskdetail:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RateTaskdetail rateTaskdetail) {
		rateTaskdetailService.updateById(rateTaskdetail);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "评估表填报-通过id删除")
	  
	//@RequiresPermissions("rate:rate_taskdetail:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		rateTaskdetailService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "评估表填报-批量删除")
	  
	//@RequiresPermissions("rate:rate_taskdetail:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.rateTaskdetailService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "评估表填报-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<RateTaskdetail> queryById(@RequestParam(name="id",required=true) String id) {
		RateTaskdetail rateTaskdetail = rateTaskdetailService.getById(id);
		if(rateTaskdetail==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(rateTaskdetail);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param rateTaskdetail
    */
   // @RequiresPermissions("rate:rate_taskdetail:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RateTaskdetail rateTaskdetail) {
        return super.exportXls(request, rateTaskdetail, RateTaskdetail.class, "评估表填报");
    }

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


	 /**
	  *   填报提交
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "评估表填报-填报提交")
	   
	 //@RequiresPermissions("rate:rate_taskdetail:wriSubOne")
	 @DeleteMapping(value = "/wriSubOne")
	 public Result<String> wriSubOne(@RequestParam(name="id",required=true) String id) {

		 RateTaskdetail rateTaskdetail = rateTaskdetailService.getById(id);
		 if(rateTaskdetail.getRateApprove().equals("0")){
		 	//不需要审核
			 rateTaskdetail.setRateTaskstatus("10");//已提交
		 }else{
		 	//需要审核
			 rateTaskdetail.setRateTaskstatus("7");//待审核
		 }

		 rateTaskdetailService.updateById(rateTaskdetail);
		 return Result.OK("填报提交成功!");
	 }

}
