package com.pj.project.tb_costomer;

import java.util.List;

import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pj.constants.UserTypeEnum;
import com.pj.utils.so.SoMap;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.pj.utils.sg.*;
import com.pj.project4sp.SP;

import com.pj.current.satoken.StpUserUtil;
import cn.dev33.satoken.annotation.SaCheckPermission;


/**
 * Controller: tb_costomer -- 客户管理
 *
 * @author qzy
 */
@RestController
@RequestMapping("/TbCostomer/")
public class TbCostomerController {

    /**
     * 底层 Service 对象
     */
    @Autowired
    TbCostomerService tbCostomerService;

    /**
     * 增
     */
    @RequestMapping("add")
    @SaCheckPermission(TbCostomer.PERMISSION_CODE)
    @Transactional(rollbackFor = Exception.class)
    public AjaxJson add(TbCostomer t) {
        tbCostomerService.add(t);
        return AjaxJson.getSuccessData(t);
    }

    /**
     * 删
     */
    @RequestMapping("delete")
    @SaCheckPermission(TbCostomer.PERMISSION_CODE)
    public AjaxJson delete(String id) {
        int line = tbCostomerService.delete(id);
        return AjaxJson.getByLine(line);
    }

    /**
     * 删 - 根据id列表
     */
    @RequestMapping("deleteByIds")
    @SaCheckPermission(TbCostomer.PERMISSION_CODE)
    public AjaxJson deleteByIds() {
        List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class);
        ids.forEach(id -> tbCostomerService.delete(id + ""));
        return AjaxJson.getByLine(1);
    }

    /**
     * 改
     */
    @RequestMapping("update")
    @SaCheckPermission(value = {TbCostomer.PERMISSION_CODE, TbCostomer.PERMISSION_INFO}, mode = SaMode.OR)
    public AjaxJson update(TbCostomer t) {
        int line = tbCostomerService.update(t);
        return AjaxJson.getByLine(line);
    }

    /**
     * 改
     */
    @RequestMapping("update/partner")
    @SaCheckPermission(TbCostomer.PARTNER_PERMISSION_CODE)
    public AjaxJson updatePartner(TbCostomer t) {
        int line = tbCostomerService.update(t);
        return AjaxJson.getByLine(line);
    }

    /**
     * 查 - 根据id
     */
    @RequestMapping("getById")
    public AjaxJson getById(String id) {
        TbCostomer t = tbCostomerService.getById(id);
        return AjaxJson.getSuccessData(t);
    }

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    @RequestMapping("getList")
    public AjaxJson getList() {
        SoMap so = SoMap.getRequestSoMap();
        String cusotmerId = StpUserUtil.getCustomerId();
        if (StrUtil.isNotEmpty(so.getString("partner")) && !StrUtil.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(), cusotmerId)) {
            so.put("id", cusotmerId);
        }
        List<TbCostomer> list = tbCostomerService.getList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    /**
     * 查集合 - 客户登录时查询自己
     */
    @RequestMapping("getOwnList")
    public AjaxJson getOwnList() {
        SoMap so = SoMap.getRequestSoMap();
        so.put("id", StpUserUtil.getCustomerId());
        List<TbCostomer> list = tbCostomerService.getList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    @RequestMapping("getAll")
    public AjaxJson getAll() {
        SoMap so = SoMap.getRequestSoMap();
        so.put("status", 1);
        so.put("judgeStatus", 2);
        List<TbCostomer> list = tbCostomerService.getList(so);
        return AjaxJson.getSuccessData(list);
    }

    /**
     * 改 - 状态(0=否,1=是)
     */
    @RequestMapping("updateStatus")
    @SaCheckPermission(TbCostomer.PERMISSION_CODE)
    public AjaxJson updateStatus(String id, int value) {
        tbCostomerService.updateStatus(id, value);

        return AjaxJson.getByLine(1);
    }


    @RequestMapping("getCurrentCustomerId")
    public AjaxJson getCurrentCustomer() {
        String id = StpUserUtil.getCustomerId();
        return AjaxJson.getSuccessData(id);
    }

    @RequestMapping("judge")
    @SaCheckPermission(TbCostomer.PERMISSION_CODE)
    public AjaxJson judge() {
        SoMap so = SoMap.getRequestSoMap();
        tbCostomerService.judge(so.getString("id"), so.getInt("judgeStatus"), so.getString("judgeContent"));
        return AjaxJson.getSuccess();
    }

    @RequestMapping("getListAndAdmin")
    public AjaxJson getListAndAdmin() {
        QueryWrapper<TbCostomer> ew = new QueryWrapper<>();
        ew.eq("judge_status", 2);
        List<TbCostomer> costomers = tbCostomerService.list(ew);
        TbCostomer tbCostomer = new TbCostomer();
        tbCostomer.setId(UserTypeEnum.PLATFORM_ADMIN.getCustomerId()).setName("广西东兴北投口岸投资有限公司");
        costomers.add(0, tbCostomer);
        return AjaxJson.getSuccessData(costomers);
    }
    @RequestMapping("getTypeList")
    public AjaxJson getTypeList(){
        return AjaxJson.getSuccessData(TbCostomer.CustomerEnum.getList());
    }

}
