package org.jeecg.modules.stm.act.main.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
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.modules.stm.act.main.entity.StCaMain;
import org.jeecg.modules.stm.act.main.service.IStCaMainService;
import org.jeecg.modules.stm.act.main.vo.StCaMainVo;
import org.jeecg.modules.stm.base.domain.service.IStDomainService;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 控制活动
 * @Author: yzh
 * @Date:   2023-03-06
 * @Version: V1.0
 */

@RestController
@RequestMapping("/act.main/stCaMain")
@Slf4j
public class StCaMainController extends JeecgController<StCaMain, IStCaMainService> {
	@Autowired
	private IStCaMainService stCaMainService;
	@Autowired
	private IStDomainService stDomainService;
	/**
	 * 分页列表查询
	 *
	 * @param stCaMain
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "控制活动-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<StCaMain>> queryPageList(StCaMain stCaMain,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		//QueryWrapper<StCaMain> queryWrapper = QueryGenerator.initQueryWrapper(stCaMain, req.getParameterMap());
		//Page<StCaMain> page = new Page<StCaMain>(pageNo, pageSize);
		IPage<StCaMain> pageList = stCaMainService.defPage(stCaMain,pageNo,pageSize,req.getParameterMap());

		//IPage<StCaMain> pageList = stCaMainService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param stCaMain
	 * @return
	 */
	@AutoLog(value = "控制活动-添加")
	  
	@RequiresPermissions("act.main:st_ca_main:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody StCaMainVo stCaMain) {
		stCaMainService.save(stCaMain);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param stCaMain
	 * @return
	 */
	@AutoLog(value = "控制活动-编辑")
	  
	@RequiresPermissions("act.main:st_ca_main:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody StCaMainVo stCaMain) {
		stCaMainService.updateById(stCaMain);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "控制活动-通过id删除")
	  
	@RequiresPermissions("act.main:st_ca_main:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		stCaMainService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "控制活动-批量删除")
	  
	@RequiresPermissions("act.main:st_ca_main:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.stCaMainService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "控制活动-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<StCaMain> queryById(@RequestParam(name="id",required=true) String id) {
		StCaMain stCaMain = stCaMainService.getById(id);
		if(stCaMain==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(stCaMain);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param stCaMain
    */
    @RequiresPermissions("act.main:st_ca_main:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StCaMain stCaMain) {
        return super.exportXls(request, stCaMain, StCaMain.class, "控制活动");
    }

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


	@GetMapping(value = "/listAll")
	public Result<Map<String,Object>> queryPageList(StCaMain stCaMain,
				 HttpServletRequest req) {
		List<StCaMain> actList = stCaMainService.list();
		List<String> domainCodeList = new ArrayList<>();
		for (StCaMain stMain : actList) {
			String domainCode = stMain.getDomainCode();
			if(domainCode!=null &&!domainCode.equals("")) {
				domainCodeList.add(domainCode);
			}
		}
		Map<String,Object> retMap = new HashMap<>();
		if(domainCodeList.size()>0) {
			Map<String, String> domainCodeNameMap = stDomainService.getAllParentCode(domainCodeList);
			retMap.put("domainNames",domainCodeNameMap);
		}
		retMap.put("actList",actList);
		return Result.OK(retMap);
	}

}
