package com.cool.store.service.impl;

import com.cool.store.entity.LicenseTransactDO;
import com.cool.store.entity.ShopInfoDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.LicenseTypeEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.ApplyLicenseMapper;
import com.cool.store.mapper.ShopInfoMapper;
import com.cool.store.request.AuditResultRequest;
import com.cool.store.response.LicenseApiResponse;
import com.cool.store.service.KdzApiService;
import com.cool.store.utils.StringUtil;
import com.cool.store.utils.poi.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

@Service
@Slf4j
public class KdzApiServiceImpl implements KdzApiService {

    @Resource
    private CommonService commonService;

    @Resource
    private ShopInfoMapper shopInfoMapper;

    @Resource
    ApplyLicenseMapper applyLicenseMapper;

    @Override
    public boolean auditResult(AuditResultRequest request) {
        if (Objects.isNull(request) || StringUtil.isBlank(request.getKdzBusinessId())) {
            throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        return commonService.getAuditService(request).auditResult(request);
    }

    @Override
    public LicenseApiResponse license(String storeNum) {
        ShopInfoDO shopInfoDO = shopInfoMapper.selectByStoreNum(storeNum);
        if (Objects.isNull(shopInfoDO)) {
            throw new ServiceException(ErrorCodeEnum.STORE_NUM_NOT_FOUND);
        }
        Long shopId = shopInfoDO.getId();
        LicenseTransactDO licenseTransactDO = applyLicenseMapper.selectByShopId(shopId);
        if (Objects.isNull(licenseTransactDO)) {
            throw new ServiceException(ErrorCodeEnum.LICENSE_NOT_EXIST);
        }
        return convert(licenseTransactDO, storeNum);
    }

    public LicenseApiResponse convert(LicenseTransactDO licenseTransactDO,
                                      String storeNum) {
        return LicenseApiResponse.builder()
                .storeBusinessAdd(licenseTransactDO.getLicenseAddress())
                .storeBusinessDate(DateUtils.dateTime(licenseTransactDO.getIssueTime()))
                .storeBusinessImg(licenseTransactDO.getCreditUrl())
                .storeBusinessName(licenseTransactDO.getBusinessLicense())
                .storeBusinessNumber(licenseTransactDO.getCreditCode())
                .storeBusinessType(Objects.requireNonNull(LicenseTypeEnum.match(licenseTransactDO.getLicenseType())).getMessage())
                .storeBusinessValidPeriod(Objects.isNull(licenseTransactDO.getValidity()) ? "9" : DateUtils.dateTime(licenseTransactDO.getValidity()))
                .storeCode(storeNum)
                .storeDirector(licenseTransactDO.getOperator())
                .storeFoodLicense(licenseTransactDO.getFoodBusinessLicenseCode())
                .storeFoodLicenseBeginDate(DateUtils.dateTime(licenseTransactDO.getFoodBusinessStartTime()))
                .storeFoodLicenseEndDate(DateUtils.dateTime(licenseTransactDO.getFoodBusinessEndTime()))
                .storeFoodLicenseBusinessAddress(licenseTransactDO.getFoodLicenseAddress())
                .storeFoodLicenseBusinessScope(licenseTransactDO.getBusinessProject())
                .storeFoodLicenseImg(licenseTransactDO.getFoodBusinessLicenseUrl())
                .storeFoodLicenseLegalRepresentative(licenseTransactDO.getFoodLicenseLegalPerson())
                .storeFoodLicenseMainBusiness(licenseTransactDO.getMainBusiness())
                .storeFoodLicenseOperatorName(licenseTransactDO.getOperator())
                .storeRemark("")
                .build();
    }
}
