package com.yueqian.xingyun.basedata.bo.address;

import com.lframework.starter.web.core.annotations.convert.EnumConvert;
import com.lframework.starter.web.core.bo.BaseBo;
import com.lframework.starter.web.core.utils.ApplicationUtil;
import com.yueqian.xingyun.basedata.entity.Address;
import com.yueqian.xingyun.basedata.entity.Customer;
import com.yueqian.xingyun.basedata.entity.Member;
import com.yueqian.xingyun.basedata.entity.Shop;
import com.yueqian.xingyun.basedata.entity.StoreCenter;
import com.yueqian.xingyun.basedata.entity.Supplier;
import com.yueqian.xingyun.basedata.enums.AddressEntityType;
import com.yueqian.xingyun.basedata.service.customer.CustomerService;
import com.yueqian.xingyun.basedata.service.member.MemberService;
import com.yueqian.xingyun.basedata.service.shop.ShopService;
import com.yueqian.xingyun.basedata.service.storecenter.StoreCenterService;
import com.yueqian.xingyun.basedata.service.supplier.SupplierService;
import com.lframework.starter.web.inner.dto.dic.city.DicCityDto;
import com.lframework.starter.web.inner.service.DicCityService;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
import lombok.Data;

@Data
public class QueryAddressBo extends BaseBo<Address> {

  /**
   * ID
   */
  @Schema(description = "ID")
  private String id;

  /**
   * 实体ID
   */
  @Schema(description = "实体ID")
  private String entityId;

  /**
   * 实体名称
   */
  @Schema(description = "实体名称")
  private String entityName;

  /**
   * 实体类型
   */
  @Schema(description = "实体类型")
  @EnumConvert
  private Integer entityType;

  /**
   * 地址类型
   */
  @Schema(description = "地址类型")
  @EnumConvert
  private Integer addressType;

  /**
   * 姓名
   */
  @Schema(description = "姓名")
  private String name;

  /**
   * 手机号
   */
  @Schema(description = "手机号")
  private String telephone;

  /**
   * 省ID
   */
  @Schema(description = "省ID")
  private String provinceId;

  /**
   * 省名称
   */
  @Schema(description = "省名称")
  private String provinceName;

  /**
   * 市ID
   */
  @Schema(description = "市ID")
  private String cityId;

  /**
   * 市名称
   */
  @Schema(description = "市名称")
  private String cityName;

  /**
   * 区ID
   */
  @Schema(description = "区ID")
  private String districtId;

  /**
   * 区名称
   */
  @Schema(description = "区名称")
  private String districtName;

  /**
   * 详细地址
   */
  @Schema(description = "详细地址")
  private String address;

  /**
   * 是否默认地址
   */
  @Schema(description = "是否默认地址")
  private Boolean isDefault;

  /**
   * 创建人ID
   */
  @Schema(description = "创建人ID")
  private String createBy;

  /**
   * 创建时间
   */
  @Schema(description = "创建时间")
  private LocalDateTime createTime;

  public QueryAddressBo() {

  }

  public QueryAddressBo(Address dto) {

    super(dto);
  }

  @Override
  protected void afterInit(Address dto) {
    DicCityService dicCityService = ApplicationUtil.getBean(DicCityService.class);
    DicCityDto province = dicCityService.findById(dto.getProvinceId());
    this.provinceName = province.getName();

    DicCityDto city = dicCityService.findById(dto.getCityId());
    this.cityName = city.getName();

    DicCityDto district = dicCityService.findById(dto.getDistrictId());
    this.districtName = district.getName();

    if (dto.getEntityType() == AddressEntityType.SC) {
      StoreCenterService storeCenterService = ApplicationUtil.getBean(StoreCenterService.class);
      StoreCenter sc = storeCenterService.findById(dto.getEntityId());
      this.entityName = sc.getName();
    } else if (dto.getEntityType() == AddressEntityType.CUSTOMER) {
      CustomerService customerService = ApplicationUtil.getBean(CustomerService.class);
      Customer customer = customerService.findById(dto.getEntityId());
      this.entityName = customer.getName();
    } else if (dto.getEntityType() == AddressEntityType.SUPPLIER) {
      SupplierService supplierService = ApplicationUtil.getBean(SupplierService.class);
      Supplier supplier = supplierService.findById(dto.getEntityId());
      this.entityName = supplier.getName();
    } else if (dto.getEntityType() == AddressEntityType.MEMBER) {
      MemberService memberService = ApplicationUtil.getBean(MemberService.class);
      Member member = memberService.findById(dto.getEntityId());
      this.entityName = member.getName();
    } else if (dto.getEntityType() == AddressEntityType.SHOP) {
      ShopService shopService = ApplicationUtil.getBean(ShopService.class);
      Shop shop = shopService.findById(dto.getEntityId());
      this.entityName = shop.getName();
    }
  }
}
