package co.yixiang.yshop.module.order.service.rechargeconfig;

import co.yixiang.yshop.module.order.controller.admin.rechargeconfig.vo.RechargeAllConfigVO;
import co.yixiang.yshop.module.order.controller.admin.rechargeconfig.vo.RechargeConfigExportReqVO;
import co.yixiang.yshop.module.order.controller.admin.rechargeconfig.vo.RechargeConfigUpdateReqVO;
import co.yixiang.yshop.module.order.controller.admin.rechargepackage.vo.RechargePackageExportReqVO;
import co.yixiang.yshop.module.order.controller.admin.rechargepackage.vo.RechargePackageRespVO;
import co.yixiang.yshop.module.order.convert.rechargeconfig.RechargeConfigConvert;
import co.yixiang.yshop.module.order.convert.rechargepackage.RechargePackageConvert;
import co.yixiang.yshop.module.order.dal.dataobject.rechargeconfig.RechargeConfigDO;
import co.yixiang.yshop.module.order.dal.dataobject.rechargepackage.RechargePackageDO;
import co.yixiang.yshop.module.order.service.rechargepackage.RechargePackageService;
import org.springframework.stereotype.Service;

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

@Service
public class RechargeConfigFacadeImpl implements RechargeConfigFacade {

    @Resource
    private RechargeConfigService rechargeConfigService;

    @Resource
    private RechargePackageService rechargePackageService;

    @Override
    public RechargeAllConfigVO getAllConfig(Integer status) {
        List<RechargeConfigDO> rechargeConfigList = rechargeConfigService.getRechargeConfigList(new RechargeConfigExportReqVO());
        RechargeConfigDO rechargeConfigDO = rechargeConfigList.get(0);
        RechargeAllConfigVO rechargeAllConfigVO = RechargeConfigConvert.INSTANCE.convert02(rechargeConfigDO);
        List<RechargePackageDO> rechargePackageList = rechargePackageService.getRechargePackageList(new RechargePackageExportReqVO().setStatus(status));
        List<RechargePackageRespVO> rechargePackageRespVOS = RechargePackageConvert.INSTANCE.convertList(rechargePackageList);
        rechargeAllConfigVO.setRechargePackageBaseVOS(rechargePackageRespVOS);
        return rechargeAllConfigVO;
    }

    @Override
    public void updateAllConfig(RechargeAllConfigVO rechargeAllConfigVO) {
        List<RechargeConfigDO> rechargeConfigList = rechargeConfigService.getRechargeConfigList(new RechargeConfigExportReqVO());
        RechargeConfigDO rechargeConfigDO = rechargeConfigList.get(0);
        RechargeConfigUpdateReqVO updateReqVO = new RechargeConfigUpdateReqVO();
        updateReqVO.setId(rechargeConfigDO.getId());
        updateReqVO.setCustomSwitch(rechargeAllConfigVO.getCustomSwitch());
        updateReqVO.setCustomMin(rechargeAllConfigVO.getCustomMin());
        updateReqVO.setContent(rechargeAllConfigVO.getContent());
        rechargeConfigService.updateRechargeConfig(updateReqVO);
        List<RechargePackageRespVO> rechargePackageBaseVOS = rechargeAllConfigVO.getRechargePackageBaseVOS();
        for (RechargePackageRespVO r : rechargePackageBaseVOS) {
            rechargePackageService.updateRechargePackage(RechargePackageConvert.INSTANCE.convert(r));
        }
    }
}
