package com.cool.store.service.impl;


import com.cool.store.dao.*;
import com.cool.store.entity.BuildInformationDO;
import com.cool.store.entity.PointInfoDO;
import com.cool.store.entity.QualificationsInfoDO;
import com.cool.store.entity.ShopInfoDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.point.ShopSubStageEnum;
import com.cool.store.enums.point.ShopSubStageStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.BuildInformationRequest;
import com.cool.store.response.BuildInformationResponse;
import com.cool.store.service.BuildInformationService;
import com.cool.store.mapper.BuildInformationMapper;
import com.cool.store.service.PreparationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
* @author EDY
* @description 针对表【xfsg_build_information(建店资料表)】的数据库操作Service实现
* @createDate 2024-10-09 14:05:52
*/
@Service
public class BuildInformationServiceImpl   implements BuildInformationService{

    @Resource
    private BuildInformationDAO buildInformationDAO;
    @Autowired
    private ShopStageInfoDAO shopStageInfoDAO;
    @Autowired
    private PointInfoDAO pointInfoDAO;
    @Autowired
    private ShopInfoDAO shopInfoDAO;
    @Resource
    PreparationService preparationService;
    @Autowired
    private QualificationsInfoDAO qualificationsInfoDAO;

    @Override
    public BuildInformationResponse getBuildInformation(Long shopId) {
        BuildInformationResponse response = new BuildInformationResponse();
        ShopInfoDO shopInfo = shopInfoDAO.getShopInfo(shopId);
        if (Objects.isNull(shopInfo)) {
            throw new ServiceException(ErrorCodeEnum.SHOP_NOT_EXIST);
        }
        response.setShopId(shopId);
        response.setShopName(shopInfo.getShopName());
        response.setShopCode(shopInfo.getShopCode());
        PointInfoDO pointInfo = pointInfoDAO.getPointInfoById(shopInfo.getPointId());
        if (Objects.nonNull(pointInfo)) {
            response.setProvince(pointInfo.getProvince());
            response.setDistrict(pointInfo.getDistrict());
            response.setCity(pointInfo.getCity());
            response.setTownship(pointInfo.getTownship());
        }
        BuildInformationDO informationDO = buildInformationDAO.selectOneByShopId(shopId);
        if (Objects.nonNull(informationDO)) {
            response.setShopContactName(informationDO.getShopContactName());
            response.setShopContactMobile(informationDO.getShopContactMobile());
            response.setBusinessHours(informationDO.getBusinessHours());
            response.setBusinessMobile(informationDO.getBusinessMobile());
            response.setDoorPhoto(informationDO.getDoorPhoto());
            response.setInStorePhoto(informationDO.getInStorePhoto());
            response.setJuridicalIdCardFront(informationDO.getJuridicalIdCardFront());
            response.setJuridicalIdCardNo(informationDO.getJuridicalIdCardNo());
            response.setJuridicalIdCardReverse(informationDO.getJuridicalIdCardReverse());
            response.setJuridicalHandheldIdCardReverse(informationDO.getJuridicalHandheldIdCardReverse());
            response.setJuridicalHandheldIdCardFront(informationDO.getJuridicalHandheldIdCardFront());
            response.setJuridicalHandheldIdCardReverse(informationDO.getJuridicalHandheldIdCardReverse());
            response.setSettlerIdCardNo(informationDO.getSettlerIdCardNo());
            response.setSettlerBankPhotoUrl(informationDO.getSettlerBankPhotoUrl());
            response.setSettlerIdCardFront(informationDO.getSettlerIdCardFront());
            response.setSettlerIdCardReverse(informationDO.getSettlerIdCardReverse());
            response.setSettlerBankNumber(informationDO.getSettlerBankNumber());
            response.setSettlerBankMobile(informationDO.getSettlerBankMobile());
            response.setSettlerBankName(informationDO.getSettlerBankName());
            response.setAuthorizationUrl(informationDO.getAuthorizationUrl());
            response.setRelationshipProve(informationDO.getRelationshipProve());
            response.setAccountOpeningPermit(informationDO.getAccountOpeningPermit());
        }
        return response;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer submitOrUpdate(BuildInformationRequest request) {
        BuildInformationDO informationDO = buildInformationDAO.selectOneByShopId(request.getShopId());
        BuildInformationDO buildInformationDO = request.toDO();
        if (Objects.isNull(informationDO)) {
            buildInformationDO.setCreateTime(new Date());
            buildInformationDO.setUpdateTime(new Date());
            shopStageInfoDAO.updateShopStageInfo(request.getShopId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_153);
            //初始化数据
            preparationService.licenseCompleted(request.getShopId());
            preparationService.sysStoreCompleted(request.getShopId());
            return buildInformationDAO.insertSelective(buildInformationDO);
        }else {
            buildInformationDO.setUpdateTime(new Date());
            return buildInformationDAO.updateByShopIdSelective(buildInformationDO);
        }

    }

    @Override
    public Integer getJoinType(Long lineId) {
        QualificationsInfoDO qualificationsInfoDO = qualificationsInfoDAO.getByLineId(lineId);
        if (Objects.isNull(qualificationsInfoDO)) {
            return null;
        }
        return qualificationsInfoDO.getJoinType();
    }


}




