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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.stm.rate.entity.RateDomain;
import org.jeecg.modules.stm.rate.entity.RateKeyitem;
import org.jeecg.modules.stm.rate.entity.RateRule;
import org.jeecg.modules.stm.rate.entity.RateStd;
import org.jeecg.modules.stm.rate.service.IRateDomainService;
import org.jeecg.modules.stm.rate.service.IRateKeyitemService;
import org.jeecg.modules.stm.rate.service.IRateRuleService;
import org.jeecg.modules.stm.rate.service.IRateStdService;

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;
import org.jeecg.modules.stm.rate.vo.RateKeyitemVo;

/**
 * @Description: 评分标准
 * @Author: jeecg-boot
 * @Date:   2023-09-21
 * @Version: V1.0
 */

@RestController
@RequestMapping("/rate/rateStd")
@Slf4j
public class RateStdController extends JeecgController<RateStd, IRateStdService> {
	@Autowired
	private IRateStdService rateStdService;
	@Autowired
	private IRateKeyitemService rateKeyitemService;
	@Autowired
	private IRateDomainService rateDomainService;
	@Autowired
	private IRateRuleService rateRuleService;
	@Autowired
	private ISysDictService iSysDictService;
	
	/**
	 * 分页列表查询
	 *
	 * @param rateStd
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "评分标准-分页列表查询")

	@GetMapping(value = "/list")
	public Result<IPage<RateStd>> queryPageList(RateStd rateStd,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<RateStd> queryWrapper = QueryGenerator.initQueryWrapper(rateStd, req.getParameterMap());
		Page<RateStd> page = new Page<RateStd>(pageNo, pageSize);
		IPage<RateStd> pageList = rateStdService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param rateStd
	 * @return
	 */
	@AutoLog(value = "评分标准-添加")

	//@RequiresPermissions("rate:rate_std:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RateStd rateStd) {
		rateStdService.save(rateStd);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param rateStd
	 * @return
	 */
	@AutoLog(value = "评分标准-编辑")

	//@RequiresPermissions("rate:rate_std:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RateStd rateStd) {
		rateStdService.updateById(rateStd);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "评分标准-通过id删除")

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

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

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

    /**
    * 导出excel
    *
    * @param request
    * @param rateStd
    */
    //@RequiresPermissions("rate:rate_std:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RateStd rateStd) {
        return super.exportXls(request, rateStd, RateStd.class, "评分标准");
    }

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


	 @RequestMapping(value = "/ruledomainkeyList", method = RequestMethod.GET)
	 public Result<List<RateKeyitemVo>> ruledomainkeyList(String id){
		 List<RateKeyitemVo> list = new ArrayList<>();
		 RateKeyitem rateKeyitem = rateKeyitemService.getById(id);
		 String domainId=rateKeyitem.getRateDomainId();
		 RateDomain rateDomain = rateDomainService.getById(domainId);
		 String domName = iSysDictService.queryDictTextByKey("rate_domain",rateDomain.getRatedomain());
		 String ruleid=rateDomain.getRuleId();
		 RateRule rateRule = rateRuleService.getById(ruleid);

		 RateKeyitemVo rateKeyitemVo = new RateKeyitemVo();

		 rateKeyitemVo.setRuleName(rateRule.getRulename());
		 rateKeyitemVo.setRatedomain(domName);
		 rateKeyitemVo.setSondomain(rateDomain.getSondomain());
		  rateKeyitemVo.setKeyitem(rateKeyitem.getKeyitem());
		 rateKeyitemVo.setRatedomainId(domainId);
		 rateKeyitemVo.setRuleId(ruleid);
		 rateKeyitemVo.setKeyitemId(id);


		 list.add(rateKeyitemVo);
		 return Result.OK(list);
	 }

}
