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.service.IRateDomainService;
import org.jeecg.modules.stm.rate.service.IRateKeyitemService;

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

/**
 * @Description: 评估要素
 * @Author: jeecg-boot
 * @Date:   2023-09-21
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/rate/rateKeyitem")
@Slf4j
public class RateKeyitemController extends JeecgController<RateKeyitem, IRateKeyitemService> {
	@Autowired
	private IRateKeyitemService rateKeyitemService;
	@Autowired
	private IRateDomainService rateDomainService;
	@Autowired
	private IRateRuleService rateRuleService;
	@Autowired
	private ISysDictService iSysDictService;
	
	/**
	 * 分页列表查询
	 *
	 * @param rateKeyitem
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "评估要素-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<RateKeyitem>> queryPageList(RateKeyitem rateKeyitem,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<RateKeyitem> queryWrapper = QueryGenerator.initQueryWrapper(rateKeyitem, req.getParameterMap());
		Page<RateKeyitem> page = new Page<RateKeyitem>(pageNo, pageSize);
		IPage<RateKeyitem> pageList = rateKeyitemService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param rateKeyitem
	 * @return
	 */
	@AutoLog(value = "评估要素-添加")
	  
	//@RequiresPermissions("rate:rate_keyitem:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RateKeyitem rateKeyitem) {
		rateKeyitemService.save(rateKeyitem);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param rateKeyitem
	 * @return
	 */
	@AutoLog(value = "评估要素-编辑")
	  
	//@RequiresPermissions("rate:rate_keyitem:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RateKeyitem rateKeyitem) {
		rateKeyitemService.updateById(rateKeyitem);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "评估要素-通过id删除")
	  
	//@RequiresPermissions("rate:rate_keyitem:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		rateKeyitemService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "评估要素-批量删除")
	  
	//@RequiresPermissions("rate:rate_keyitem:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.rateKeyitemService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "评估要素-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<RateKeyitem> queryById(@RequestParam(name="id",required=true) String id) {
		RateKeyitem rateKeyitem = rateKeyitemService.getById(id);
		if(rateKeyitem==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(rateKeyitem);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param rateKeyitem
    */
   // @RequiresPermissions("rate:rate_keyitem:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RateKeyitem rateKeyitem) {
        return super.exportXls(request, rateKeyitem, RateKeyitem.class, "评估要素");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
   // @RequiresPermissions("rate:rate_keyitem:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RateKeyitem.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(id);
		 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.setRuleId(ruleid);
		 rateKeyitemVo.setRatedomainId(id);
		// rateKeyitemVo.setKeyitem(rateKeyitem.getKeyitem());


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

}
