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.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.stm.rate.entity.RateDomain;
import org.jeecg.modules.stm.rate.entity.RateKeyitem;
import org.jeecg.modules.stm.rate.entity.RateStd;
import org.jeecg.modules.stm.rate.service.IRateDomainService;

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

/**
 * @Description: 领域
 * @Author: jeecg-boot
 * @Date:   2023-09-21
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/rate/rateDomain")
@Slf4j
public class RateDomainController extends JeecgController<RateDomain, IRateDomainService> {
	@Autowired
	private IRateDomainService rateDomainService;
	@Autowired
	private IRateKeyitemService rateKeyitemService;

	@Autowired
	private IRateStdService rateStdService;
	
	/**
	 * 分页列表查询
	 *
	 * @param rateDomain
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "领域-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<RateDomain>> queryPageList(RateDomain rateDomain,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<RateDomain> queryWrapper = QueryGenerator.initQueryWrapper(rateDomain, req.getParameterMap());
		Page<RateDomain> page = new Page<RateDomain>(pageNo, pageSize);
		IPage<RateDomain> pageList = rateDomainService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param rateDomain
	 * @return
	 */
	@AutoLog(value = "领域-添加")
	  
	//@RequiresPermissions("rate:rate_domain:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RateDomain rateDomain) {
		rateDomainService.save(rateDomain);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param rateDomain
	 * @return
	 */
	@AutoLog(value = "领域-编辑")
	  
	//@RequiresPermissions("rate:rate_domain:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RateDomain rateDomain) {
		rateDomainService.updateById(rateDomain);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "领域-通过id删除")
	  
	//@RequiresPermissions("rate:rate_domain:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		rateDomainService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "领域-批量删除")
	  
	//@RequiresPermissions("rate:rate_domain:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.rateDomainService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "领域-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<RateDomain> queryById(@RequestParam(name="id",required=true) String id) {
		RateDomain rateDomain = rateDomainService.getById(id);
		if(rateDomain==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(rateDomain);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param rateDomain
    */
    //@RequiresPermissions("rate:rate_domain:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RateDomain rateDomain) {
        return super.exportXls(request, rateDomain, RateDomain.class, "领域");
    }

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

	 /**
	  *  编辑
	  *
	  * @param rateDomain
	  * @return
	  */
	 @AutoLog(value = "领域-复制")
	   
	 //@RequiresPermissions("rate:rate_domain:edit")
	 @RequestMapping(value = "/editCopy", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> editCopy(@RequestBody RateDomain rateDomain) {
	 	//rateDomainService.updateById(rateDomain);
		 String ruleId=rateDomain.getRuleId();
		 if(rateDomain.getRatedomain()!=null&&!rateDomain.getRatedomain().equals("")) {
			 List domaindis = Arrays.asList(rateDomain.getRatedomain().split(","));
			 int domainindex=1;
			 for (int i = 0; i < domaindis.size(); i++) {
				 String domainid = (String) domaindis.get(i);

				 //1 根据得到子领域id，
				 RateDomain oldrateDomain = rateDomainService.getById(domainid);
				 //RateDomain newrateDomain = new RateDomain();
				 //newrateDomain.setRatedomain(oldrateDomain.getRatedomain());
				// newrateDomain.setSondomain(oldrateDomain.getSondomain());
				 oldrateDomain.setRuleId(ruleId);
				 oldrateDomain.setId(null);
				 oldrateDomain.setValindex(domainindex);
				 rateDomainService.save(oldrateDomain);
				 String newdomainid=oldrateDomain.getId();
				 System.out.println("newdomainid "+newdomainid);

				 QueryWrapper<RateKeyitem> keyitemWrapper = new QueryWrapper<>();
				 keyitemWrapper.eq("rate_domain_id",domainid);
				 List<RateKeyitem> keyitemlist = rateKeyitemService.list(keyitemWrapper);

				 for(int j=0;j<keyitemlist.size();j++) {
					 RateKeyitem keyitemmx = keyitemlist.get(j);
					 String keyitemId=keyitemmx.getId();
					 keyitemmx.setRateDomainId(newdomainid);
					 keyitemmx.setId(null);
					 rateKeyitemService.save(keyitemmx);
					 String newRateKeyitemid= keyitemmx.getId();

					 System.out.println("newRateKeyitemid "+newRateKeyitemid);

					 QueryWrapper<RateStd> rateStdQueryWrapper  = new QueryWrapper<>();
					 rateStdQueryWrapper.eq("rate_keyitem_id",keyitemId);;
					 List<RateStd> stdlist  = rateStdService.list(rateStdQueryWrapper);
					 for(int k=0;k<stdlist.size();k++){
						 RateStd rmStdVal=stdlist.get(k);
						 rmStdVal.setId(null);
						 rmStdVal.setRateKeyitemId(newRateKeyitemid);
						 rateStdService.save(rmStdVal);


					 }




					// System.out.println("keyitemmx.getKeyitem "+keyitemmx.getKeyitem());


				 }



				 domainindex++;



			 }

		 }
		 return Result.OK("复制领域成功!");
	 }

}
