package org.jeecg.modules.stm.wb.manage.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 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.stm.wb.manage.entity.WbCompanyDetail;
import org.jeecg.modules.stm.wb.manage.entity.WbCompanyThirdApi;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.model.TreeSelectModel;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.jeecg.modules.stm.wb.manage.service.IWbCompanyDetailService;
import org.jeecg.modules.stm.wb.manage.service.IWbCompanyThirdApiService;

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

/**
 * @Description: wb_company_detail
 * @Author: jeecg-boot
 * @Date:   2024-03-09
 * @Version: V1.0
 */

@RestController
@RequestMapping("/wb.manage/wbCompanyDetail")
@Slf4j
public class WbCompanyDetailController extends JeecgController<WbCompanyDetail, IWbCompanyDetailService> {
	@Autowired
	private IWbCompanyDetailService wbCompanyDetailService;
	@Autowired
	private ISysDictItemService sysDictItemService;
	@Autowired
	private ISysDictService sysDictService;
	@Autowired
	private IWbCompanyThirdApiService wbCompanyThirdApiService;
	/**
	 * 分页列表查询
	 *
	 * @param wbCompanyDetail
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "wb_company_detail-分页列表查询")

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

	@RequiresPermissions("wb.manage:wb_company_detail:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody WbCompanyDetail wbCompanyDetail) {
		wbCompanyDetailService.save(wbCompanyDetail);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param wbCompanyDetail
	 * @return
	 */
	@AutoLog(value = "wb_company_detail-编辑")

	@RequiresPermissions("wb.manage:wb_company_detail:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody WbCompanyDetail wbCompanyDetail) {
		wbCompanyDetailService.updateById(wbCompanyDetail);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "wb_company_detail-通过id删除")

	@RequiresPermissions("wb.manage:wb_company_detail:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		wbCompanyDetailService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "wb_company_detail-批量删除")

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

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

    /**
    * 导出excel
    *
    * @param request
    * @param wbCompanyDetail
    */
    @RequiresPermissions("wb.manage:wb_company_detail:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WbCompanyDetail wbCompanyDetail) {
        return super.exportXls(request, wbCompanyDetail, WbCompanyDetail.class, "wb_company_detail");
    }

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

	 /**
	  * 通过公司ID查找相关信息
	  */
	 @GetMapping(value = "/queryDetailByCid")
	 public Result<List<WbCompanyDetail>> queryDetailByCid(@RequestParam(name="apiId",required=true) Integer apiId,
																		@RequestParam(name="cid",required=true) Integer cid) {
		 QueryWrapper<WbCompanyDetail> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("cid",cid).eq("api_id",apiId).orderByAsc("api_id");
		 List<WbCompanyDetail> detailList = wbCompanyDetailService.list(queryWrapper);
		 return Result.OK(detailList);
	 }


	@GetMapping(value = "/queryApiTreeForDetail")
	public Result<List<TreeSelectModel>> queryApiTreeForDetail() {
		QueryWrapper<SysDict> queryDictWrapper = new QueryWrapper<>();
		queryDictWrapper.eq("dict_code","wb_api_tp");
		SysDict sysDict =sysDictService.getOne(queryDictWrapper);
		List<TreeSelectModel> retTreeList = new ArrayList<>();
		if(sysDict!=null) {
			QueryWrapper<SysDictItem> queryItemWrapper = new QueryWrapper<>();
			queryItemWrapper.eq("dict_id",sysDict.getId());
			List<SysDictItem> itemList = sysDictItemService.list(queryItemWrapper);

			QueryWrapper<WbCompanyThirdApi> queryWrapper = new QueryWrapper<>();
			//queryWrapper.ne("weight",0);
			List<WbCompanyThirdApi> apiList = wbCompanyThirdApiService.list(queryWrapper);
			Map<Integer,List<WbCompanyDetail>> resultMap = new HashMap<>();
			for(SysDictItem item:itemList) {
				TreeSelectModel parentTreeSelectModel = new TreeSelectModel();
				parentTreeSelectModel.setTitle(item.getItemText());
				parentTreeSelectModel.setCode(item.getItemValue());
				parentTreeSelectModel.setKey("0_"+item.getItemValue());
				parentTreeSelectModel.setValue(item.getItemValue());
				parentTreeSelectModel.setLeaf(false);
				parentTreeSelectModel.setParentId("0");
				List<TreeSelectModel> children = new ArrayList<>();
				for(WbCompanyThirdApi api:apiList) {
					if(api.getTp().intValue()==Integer.parseInt(item.getItemValue())) {
						TreeSelectModel treeSelectModel = new TreeSelectModel();
						treeSelectModel.setTitle(api.getName());
						treeSelectModel.setKey(api.getId().toString());
						treeSelectModel.setCode(api.getApiId().toString());
						treeSelectModel.setValue(api.getApiId().toString());
						treeSelectModel.setParentId(item.getId());
						treeSelectModel.setLeaf(true);
						children.add(treeSelectModel);
					}
				}
				if(children.size()>0) {
					parentTreeSelectModel.setChildren(children);
					retTreeList.add(parentTreeSelectModel);
				}
			}
		}
		return  Result.OK(retTreeList);
	}

}
