package com.pj.project.tb_costomer;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.pj.project4sp.global.BusinessException;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;

import lombok.Data;
import lombok.Getter;
import lombok.experimental.Accessors;
import org.omg.CORBA.IRObject;

/**
 * Model: tb_costomer -- 客户管理
 *
 * @author qzy
 */
@Data
@Accessors(chain = true)
@TableName(TbCostomer.TABLE_NAME)
@EqualsAndHashCode(callSuper = false)
public class TbCostomer extends Model<TbCostomer> implements Serializable {

    // ---------- 模块常量 ----------
    /**
     * 序列化版本id
     */
    private static final long serialVersionUID = 1L;
    /**
     * 此模块对应的表名
     */
    public static final String TABLE_NAME = "tb_costomer";
    /**
     * 此模块对应的权限码
     */
    public static final String PERMISSION_CODE = "tb-costomer";
    public static final String PARTNER_PERMISSION_CODE = "tb-partner";
    public static final String PERMISSION_INFO = "tb-costomer-maintain";

    // ---------- 表中字段 ----------
    /**
     * 主键
     */
    private String id;

    /**
     * 名称
     */
    private String name;

    /**
     * 联系号码
     */
    private String phone;

    /**
     * 负责人
     */
    private String dutyPeople;

    /**
     * 地址id
     */
    private String addressIds;

    /**
     * 地址
     */
    private String addressStr;

    /**
     * 营业执照
     */
    private String businessLicence;

    /**
     * 状态(0=否,1=是)
     */
    private Integer status;

    /**
     * 创建时间
     */
    private Date creareTime = null;

    /**
     * 审核状态(1=未审核,2审核通过,3审核不通过)
     */
    private Integer judgeStatus = 1;

    /**
     * 审核时间
     */
    private Date judgeTime = null;

    /**
     * 审核意见
     */
    private String judgeContent;
    /**
     * 结账方式（1=现结,2=月结）[j]
     */
    private int payType;
    /**
     * 类型0、理货员；1、消杀；2、装卸
     */
    private String type = "0";

    private String businessType = "2";

    @TableField(exist = false)
    private String typeDes;

    public String getTypeDes() {
        String type = this.type;
        StringBuilder sb = new StringBuilder();
        StrUtil.splitTrim(type, ",")
                .forEach(t -> {
                    sb.append(CustomerEnum.getDesc(t)).append("、");
                });
        return sb.substring(0, sb.lastIndexOf("、"));
    }

    @TableField(exist = false)
    private String businessTypeDes;
    public String getBusinessTypeDes() {
        String businessType = this.businessType;
        StringBuilder sb = new StringBuilder();
        StrUtil.splitTrim(businessType, ",")
                .forEach(bt -> {
                    sb.append(BusinessTypeEnum.getDesc(bt)).append("、");
                });
        return sb.substring(0, sb.lastIndexOf("、"));
    }

    @Getter
    @AllArgsConstructor
    public static enum CustomerEnum {
        BUSINESS_TYPE("0", "理货员"),
        DISINFECT_TYPE("1", "消杀"),
        TAKE_TYPE("2", "装卸"),
        HESUAN_TYPE("3", "核酸"),
        ;
        private String type;
        private String desc;

        public static String getDesc(String type) {
            return Arrays.stream(CustomerEnum.values()).filter(customerEnum -> customerEnum.getType().equals(type))
                    .findFirst().orElseThrow(() -> new BusinessException("不存在")).getDesc();
        }

        public static List<Map<String, String>> getList() {
            List<Map<String, String>> list = new ArrayList<>();
            for (CustomerEnum customerEnum : CustomerEnum.values()) {
                if (customerEnum.getType().equals("0")) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("id", customerEnum.getType());
                map.put("name", customerEnum.getDesc());
                list.add(map);
            }
            return list;
        }

    }

    @Getter
    @AllArgsConstructor
    public static enum BusinessTypeEnum {
        CAR_DISINCLE("1","其他业务"),
        HOLD_CAR("2","整车业务"),
        ;
        private String code;
        private String desc;

        public static String getDesc(String code) {
            return Arrays.stream(BusinessTypeEnum.values()).filter(businessTypeEnum -> businessTypeEnum.getCode().equals(code))
                    .findFirst().orElseThrow(() -> new BusinessException("不存在")).getDesc();
        }

    }


}
