package cn.iocoder.yudao.module.system.service.biz;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.OutboundRefundConfigPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.OutboundRefundConfigDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.OutboundRefundItemDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.OutboundRefundConfigMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.OutboundRefundItemMapper;
import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper;
import cn.iocoder.yudao.module.system.enums.common.BooleanEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.Date;
import java.util.List;


/**
 * 外出退费配置 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class OutboundRefundConfigServiceImpl implements OutboundRefundConfigService {

    @Resource
    private OutboundRefundConfigMapper outboundRefundConfigMapper;
    @Resource
    private OutboundRefundItemMapper outboundRefundItemMapper;
    @Resource
    private TenantMapper tenantMapper;


    @Override
    public Long createOutboundRefundConfig(OutboundRefundConfigDO createReqVO) {
        createReqVO.setCreatedTime(new Date());
        createReqVO.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
        outboundRefundConfigMapper.insert(createReqVO);

        List<OutboundRefundItemDO> items = createReqVO.getItems();
        for (OutboundRefundItemDO item : items) {
            item.setOutboundRefundConfigId(createReqVO.getId());
            outboundRefundItemMapper.insert(item);
        }
        return createReqVO.getId();
    }

    @Override
    public void updateOutboundRefundConfig(OutboundRefundConfigDO updateReqVO) {
        outboundRefundConfigMapper.updateById(updateReqVO);

        outboundRefundItemMapper.delete(new LambdaQueryWrapperX<OutboundRefundItemDO>()
                .eq(OutboundRefundItemDO::getOutboundRefundConfigId, updateReqVO.getId()));

        List<OutboundRefundItemDO> items = updateReqVO.getItems();
        for (OutboundRefundItemDO item : items) {
            item.setOutboundRefundConfigId(updateReqVO.getId());
            outboundRefundItemMapper.insert(item);
        }

    }

    @Override
    public void deleteOutboundRefundConfig(Long id) {
        outboundRefundConfigMapper.deleteById(id);

        outboundRefundItemMapper.delete(new LambdaQueryWrapperX<OutboundRefundItemDO>()
                .eq(OutboundRefundItemDO::getOutboundRefundConfigId, id));
    }

    @Override
    public OutboundRefundConfigDO getOutboundRefundConfig(Long id) {
        OutboundRefundConfigDO outboundRefundConfig = outboundRefundConfigMapper.selectById(id);
        if (outboundRefundConfig != null) {
            List<OutboundRefundItemDO> outboundRefundItems = outboundRefundItemMapper.selectList(new LambdaQueryWrapperX<OutboundRefundItemDO>()
                    .eq(OutboundRefundItemDO::getOutboundRefundConfigId, id));
            outboundRefundConfig.setItems(outboundRefundItems);
        }
        return outboundRefundConfig;
    }

    @Override
    public PageResult<OutboundRefundConfigDO> getOutboundRefundConfigPage(OutboundRefundConfigPageReqVO pageReqVO) {
        PageResult<OutboundRefundConfigDO> pageResult = outboundRefundConfigMapper.selectPage(pageReqVO);
        for (OutboundRefundConfigDO outboundRefundConfigDO : pageResult.getList()) {
            outboundRefundConfigDO.setTenantName(tenantMapper.selectById(outboundRefundConfigDO.getTenantId()).getName());
        }
        return pageResult;
    }

    @Override
    public List<OutboundRefundConfigDO> findList(Long[] tenantIds) {
        return outboundRefundConfigMapper.selectList(new LambdaQueryWrapperX<OutboundRefundConfigDO>()
                .inIfPresent(OutboundRefundConfigDO::getTenantId, tenantIds)
                .eq(OutboundRefundConfigDO::getStatus, BooleanEnum.TRUE.getValue()));
    }
}