package com.sifang.modules.customer.Service;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.dao.CustomerEntryTenantDao;
import com.sifang.modules.customer.entity.CustomerEntryTenant;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.entity.Operator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerEntryTenantService extends BaseService {

    @Autowired
    private CustomerEntryTenantDao tenantDao;

    /**
     * 获取客户列表
     */
    public Result getListByUserAuth(Operator operator, String tenantType) {
        List<Record> list = tenantDao.findListByUserAuth(operator, tenantType);
        return Result.success(JsonKit.toJson(list));
    }

    /**
     * 获取客户详情
     */
    public Result getDetailById(String id) {
        CustomerEntryTenant tenant = tenantDao.get(id);
        if (tenant == null) {
            tenant = new CustomerEntryTenant();
        }
        String birthDayDate = "";
        if (tenant.getBirthdayDate() != null) {
            birthDayDate = DateUtils.formatDate(tenant.getBirthdayDate(), "yyyy-MM-dd");
        }
        String icValidityBegin = "";
        if (tenant.getIcValidityBegin() != null) {
            icValidityBegin = DateUtils.formatDate(tenant.getIcValidityBegin(), "yyyy-MM-dd");
        }
        String tenantIcValidityEnd = "";
        if (tenant.getIcValidityEnd() != null) {
            tenantIcValidityEnd = DateUtils.formatDate(tenant.getIcValidityEnd(), "yyyy-MM-dd");
        }
        tenant.setBirthdayDate1(birthDayDate);
        tenant.setIcValidityBegin1(icValidityBegin);
        tenant.setIcValidityEnd1(tenantIcValidityEnd);
        return Result.success(tenant);
    }
}
