package com.yifu.cloud.plus.v1.yifu.archives.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TCutsomerDataPermisson;
import com.yifu.cloud.plus.v1.yifu.archives.service.TCutsomerDataPermissonService;
import com.yifu.cloud.plus.v1.yifu.archives.service.TSettleDomainService;
import com.yifu.cloud.plus.v1.yifu.archives.vo.TCutsomerDataPermissonVo;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.log.annotation.SysLog;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;


/**
 * 客户数据权限表
 *
 * @author fxj
 * @date 2022-07-13 15:20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tcutsomerdatapermisson")
@Tag(name = "客户数据权限表")
public class TCutsomerDataPermissonController {

  private final TCutsomerDataPermissonService tCutsomerDataPermissonService;
  private final TSettleDomainService settleDomainService;

  /**
   * 简单分页查询
   * @param page 分页对象
   * @param tCutsomerDataPermisson 客户数据权限表
   * @return
   */
  @Operation(description = "简单分页查询")
  @GetMapping("/page")
  public R<IPage<TCutsomerDataPermissonVo>> getTCutsomerDataPermissonPage(Page<TCutsomerDataPermissonVo> page, TCutsomerDataPermissonVo tCutsomerDataPermisson) {
    return  new R<>(tCutsomerDataPermissonService.getTCutsomerDataPermissonPage(page,tCutsomerDataPermisson));
  }
  /**
   * @Author fxj
   * @Description  获取实体的权限: type-类型0:客户1:合同2:结算主体(不传默认2结算主体)
   * @Date 15:20 2022/7/13
   * @Param
   * @return
  **/
  @Operation(description = "获取实体的权限: type-类型0:客户1:合同2:结算主体(不传默认2结算主体)")
  @GetMapping("/getPermissonByTypeAndId")
  public R<IPage<TCutsomerDataPermisson>> getTCutsomerDataPermissonByTypeAndId(String id, String type) {
    if(Common.isEmpty(id)){
      return R.failed("实体id");
    }
    TCutsomerDataPermisson tp = new TCutsomerDataPermisson();

    if(CommonConstants.dingleDigitStrArray[0].equals(type)){
      tp.setCustomerId(id);
    }else if(CommonConstants.dingleDigitStrArray[1].equals(type)){
      tp.setContractId(id);
    }else{
      tp.setSettleDomainId(id);
    }
    return  new R(tCutsomerDataPermissonService.list(Wrappers.lambdaQuery(tp)));
  }

	/**
	 * @Author fxj
	 * @Description 获取实体的权限: type-类型0:客户1:合同2:结算主体(不传默认2结算主体)
	 * @Date 15:21 2022/7/13
	 * @Param
	 * @return
	**/
  @Operation(description = "权限复制 权限同结算主体分配客服（wxhr:tsettledomain_assign_customerservice）")
  @GetMapping("/copy")
  @PreAuthorize("@pms.hasPermission('wxhr:tsettledomain_assign_customerservice')")
  public R insertForCopyCutsomerDataPermisson(String[] srcUser, String[] beUser) {
     YifuUser user = SecurityUtils.getUser();
      R result = tCutsomerDataPermissonService.insertForCopyCutsomerDataPermisson(srcUser, beUser, user);
      if(CommonConstants.SUCCESS.equals(result.getCode())){//权限刷新
          settleDomainService.removeSettleCache(user, Arrays.asList(beUser));
      }
      return result ;
  }

  /**
   * 通过id查询单条记录
   * @param id
   * @return R
   */
  @Operation(description = "id查询")
  @GetMapping("/{id}")
  public R<TCutsomerDataPermisson> getById(@PathVariable("id") String id){
    return new R<>(tCutsomerDataPermissonService.getById(id));
  }

  /**
   * 新增记录
   * @param tCutsomerDataPermisson
   * @return R
   */
  @Operation(description = "新增")
  @SysLog("新增客户数据权限表")
  @PostMapping
  @PreAuthorize("@pms.hasPermission('wxhr:tcutsomerdatapermisson_add')")
  public R save(@RequestBody TCutsomerDataPermisson tCutsomerDataPermisson){
    return new R<>(tCutsomerDataPermissonService.save(tCutsomerDataPermisson));
  }

  /**
   * 修改记录
   * @param tCutsomerDataPermisson
   * @return R
   */
  @Operation(description = "修改")
  @SysLog("修改客户数据权限表")
  @PutMapping
  @PreAuthorize("@pms.hasPermission('wxhr:tcutsomerdatapermisson_edit')")
  public R update(@RequestBody TCutsomerDataPermisson tCutsomerDataPermisson){
    return new R<>(tCutsomerDataPermissonService.updateById(tCutsomerDataPermisson));
  }

  /**
   * 通过id删除一条记录
   * @param id
   * @return R
   */
  @Operation(description = "删除(wxhr:tcutsomerdatapermisson_del)")
  @SysLog("删除客户数据权限表")
  @DeleteMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('wxhr:tcutsomerdatapermisson_del')")
  public R removeById(@PathVariable String id){
      YifuUser user = SecurityUtils.getUser();
      TCutsomerDataPermisson tdp = tCutsomerDataPermissonService.getById(id);
      if(null == tdp){
          return new R<>("权限已删除！");
      }
      //刷新缓存
      settleDomainService.removeSettleCache(user, Arrays.asList(tdp.getBePermissonUser()));
      return new R<>(tCutsomerDataPermissonService.removeById(id));
  }

}
