package org.jeecg.modules.city.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
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.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.city.entity.CsPtCity;
import org.jeecg.modules.city.service.ICsPtCityService;
import org.jeecg.modules.city.vo.CsPtCityTreeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * @Description: cs_pt_city
 * @Author: jeecg-boot
 * @Date:   2023-10-12
 * @Version: V1.0
 */
@Api(tags="省市县行政区划")
@RestController
@RequestMapping("/city/csPtCity")
@Slf4j
public class CsPtCityController extends JeecgController<CsPtCity, ICsPtCityService> {
	@Autowired
	private ICsPtCityService csPtCityService;
	
	/**
	 * 分页列表查询
	 *
	 * @param csPtCity
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "cs_pt_city-分页列表查询")
	@ApiOperation(value="cs_pt_city-分页列表查询", notes="cs_pt_city-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CsPtCity>> queryPageList(CsPtCity csPtCity,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CsPtCity> queryWrapper = QueryGenerator.initQueryWrapper(csPtCity, req.getParameterMap());
		Page<CsPtCity> page = new Page<CsPtCity>(pageNo, pageSize);
		IPage<CsPtCity> pageList = csPtCityService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param csPtCity
	 * @return
	 */
	@AutoLog(value = "cs_pt_city-添加")
	@ApiOperation(value="cs_pt_city-添加", notes="cs_pt_city-添加")
	@RequiresPermissions("city:cs_pt_city:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CsPtCity csPtCity) {
		csPtCityService.save(csPtCity);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param csPtCity
	 * @return
	 */
	@AutoLog(value = "cs_pt_city-编辑")
	@ApiOperation(value="cs_pt_city-编辑", notes="cs_pt_city-编辑")
	@RequiresPermissions("city:cs_pt_city:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CsPtCity csPtCity) {
		csPtCityService.updateById(csPtCity);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "cs_pt_city-通过id删除")
	@ApiOperation(value="cs_pt_city-通过id删除", notes="cs_pt_city-通过id删除")
	@RequiresPermissions("city:cs_pt_city:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		csPtCityService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "cs_pt_city-批量删除")
	@ApiOperation(value="cs_pt_city-批量删除", notes="cs_pt_city-批量删除")
	@RequiresPermissions("city:cs_pt_city:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.csPtCityService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "cs_pt_city-通过id查询")
	@ApiOperation(value="cs_pt_city-通过id查询", notes="cs_pt_city-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CsPtCity> queryById(@RequestParam(name="id",required=true) String id) {
		CsPtCity csPtCity = csPtCityService.getById(id);
		if(csPtCity==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(csPtCity);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param csPtCity
    */
    @RequiresPermissions("city:cs_pt_city:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CsPtCity csPtCity) {
        return super.exportXls(request, csPtCity, CsPtCity.class, "cs_pt_city");
    }

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

	 @ApiOperation(value="省市树结构查询结构", notes="省市树结构查询结构")
	 @GetMapping(value = "/listTree")
	 public Result<List<CsPtCityTreeVO>> listTree(HttpServletRequest req,
												  @ApiParam(value = "value节点值是否取name值 1是 0否", required = false)
												  @RequestParam(name = "valueType", required = false)String valueType) {
		 String cachekeyPrix = "key_";
		 Map<String,CsPtCityTreeVO> dataCache = new HashMap<>();
		 CsPtCityTreeVO csPtCityTreeVORoot = new CsPtCityTreeVO();
		 csPtCityTreeVORoot.setLabel("根节点");
		 csPtCityTreeVORoot.setValue("0");
		 csPtCityTreeVORoot.setCode(0);
		 csPtCityTreeVORoot.setName("根节点");
		 dataCache.put(cachekeyPrix + csPtCityTreeVORoot.getCode(),csPtCityTreeVORoot);

		 List<CsPtCity> list = csPtCityService.lambdaQuery().orderByAsc(CsPtCity::getCode).list();//全部返回
		 list.forEach(csPtCity1 -> {
			 CsPtCityTreeVO csPtCityTreeVO = new CsPtCityTreeVO();
			 BeanUtils.copyProperties(csPtCity1, csPtCityTreeVO);
			 csPtCityTreeVO.setLabel(csPtCity1.getName());
			 if("1".equalsIgnoreCase(valueType)){
				 csPtCityTreeVO.setValue(String.valueOf(csPtCity1.getName()));
			 }else {
				 csPtCityTreeVO.setValue(String.valueOf(csPtCity1.getCode()));
			 }


			 if(dataCache.containsKey(cachekeyPrix + csPtCity1.getPCode())){
				 CsPtCityTreeVO csPtCityTreeVOParent = dataCache.get(cachekeyPrix + csPtCity1.getPCode());
				 List<CsPtCityTreeVO> children = csPtCityTreeVOParent.getChildren();
				 if(children == null){
					 children = new ArrayList<>();
					 csPtCityTreeVOParent.setChildren(children);
				 }
				 children.add(csPtCityTreeVO);
			 }
			 dataCache.put(cachekeyPrix + csPtCityTreeVO.getCode(),csPtCityTreeVO);
		 });
		 return Result.OK(csPtCityTreeVORoot.getChildren());
	 }

}
