package com.bonus.canteen.core.customer.controller;

import cn.hutool.core.lang.tree.Tree;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.canteen.core.common.base.BaseController;
import com.bonus.canteen.core.common.utils.HeaderFetchUtil;
import com.bonus.canteen.core.customer.dto.CustOrgPageDTO;
import com.bonus.canteen.core.customer.service.CustOrgService;
import com.bonus.canteen.core.customer.vo.CustOrgVO;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.constant.GlobalConstants;
import com.bonus.common.houqin.utils.AesEncryptUtil;
import com.bonus.common.houqin.utils.SM4EncryptUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping({"/custOrg"})
@Api("customer-组织机构(部门)控制器")
public class CustOrgController extends BaseController {
   private static final Logger log = LoggerFactory.getLogger(CustOrgController.class);
   @Autowired
   @Lazy
   private CustOrgService custOrgService;
   @Autowired
   @Lazy
   private AesEncryptUtil aesEncryptUtil;

   @ApiOperation("分页查询组织")
   @PostMapping({"/queryPageCustOrg"})
   public Page<CustOrgVO> queryPageCustOrg(@RequestHeader Map<String, String> requestHeader, @RequestBody CustOrgPageDTO content) {
      if (!GlobalConstants.CANTEEN_YIZHAN.equals(SM4EncryptUtils.sm4Decrypt(HeaderFetchUtil.getSign(requestHeader)))) {
         throw new ServiceException("访问缺少认证信息");
      }
      Page<CustOrgVO> custOrgVOPage = this.custOrgService.pageCustOrgByParams(new Page(content.getCurrent(), content.getSize()), content);
      Iterator var4 = custOrgVOPage.getRecords().iterator();

      while(var4.hasNext()) {
         CustOrgVO custOrgVO = (CustOrgVO)var4.next();
         custOrgVO.setOrgTel(this.aesEncryptUtil.aesEncrypt(custOrgVO.getOrgTel()));
      }
      return custOrgVOPage;
   }


//   @RequiresGuest
//   @ApiOperation("根据id查询组织")
//   @GetMapping({"/queryCustOrgById"})
//   public CustOrg queryCustOrgById(LeRequest<String> request) {
//      Map<String, JSONObject> contentMap = (Map)this.gson.fromJson((String)request.getContent(), Map.class);
//      return this.custOrgService.getCustOrg(((JSONObject)contentMap.get("object")).getLong("id"));
//   }
//
//   @RequiresPermissions({"cust:org:add"})
//   @RequiresAuthentication
//   @ApiOperation("新增")
//   @PostMapping({"/addCustOrg"})
//   public void addCustOrg(@Validated({ValidationGroups.Insert.class}) @RequestBody LeRequest<CustOrg> request) {
//      CustOrg content = (CustOrg)request.getContent();
//      content.setOrgTel(this.aesEncryptUtil.aesDecode(content.getOrgTel()));
//      this.custOrgService.addCustOrg(content);
//   }
//
//   @RequiresPermissions({"cust:org:edit"})
//   @ApiOperation("根据id修改")
//   @PutMapping({"/updateCustOrgById"})
//   @RequiresAuthentication
//   public void updateCustOrgById(@Validated({ValidationGroups.Update.class}) @RequestBody LeRequest<CustOrg> request) {
//      CustOrg content = (CustOrg)request.getContent();
//      content.setOrgTel(this.aesEncryptUtil.aesDecode(content.getOrgTel()));
//      this.custOrgService.updateOrg(content);
//   }
//
//   @RequiresPermissions({"cust:org:del"})
//   @ApiOperation("根据组织id删除")
//   @DeleteMapping({"/deleteCustOrgByOrgId"})
//   @RequiresAuthentication
//   public void deleteCustOrgByOrgId(@Validated({ValidationGroups.Delete.class}) @RequestBody LeRequest<CustOrg> request) {
//      this.custOrgService.deleteByOrdId(((CustOrg)request.getContent()).getOrgId());
//   }
//
//   @ApiOperation("系统组织树")
//   @GetMapping({"/system/tree"})
//   @RequiresGuest
//   public List<Tree<Long>> getSystemOrgTree() {
//      return this.custOrgService.getSystemOrgTree();
//   }
//
//   @ApiOperation("商户权限组织树")
//   @GetMapping({"/tenant/tree"})
//   @RequiresAuthentication
//   public List<Tree<Long>> getTenantOrgTree() {
//      return this.custOrgService.getTenantOrgTree();
//   }
//
//   @ApiOperation("查询组织下默认就餐地")
//   @GetMapping({"/query/diningPlace"})
//   @RequiresGuest
//   @RequiresAuthentication
//   public JsonNode queryDiningPlace(@RequestParam("orgId") Long orgId) {
//      return this.custOrgService.queryDiningPlaceByOrgId(orgId);
//   }
//
//   @ApiOperation("根据superId查询懒加载子级组织树")
//   @PostMapping({"/lazy/org/tree"})
//   public List<CustOrgVO> getPlaceLazyTree(@RequestBody LeRequest<CustOrgTreeDTO> request) {
//      return this.custOrgService.getOrgLazyTree((CustOrgTreeDTO)request.getContent());
//   }
//
//   @ApiOperation("组织树同层级拖动排序")
//   @PostMapping({"/dragOrgSort"})
//   public void dragOrgSort(@RequestBody LeRequest<CustOrgTreeDTO> request) {
//      this.custOrgService.dragOrgSort((CustOrgTreeDTO)request.getContent());
//   }
}
