package com.hzs.system.config.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.domain.serviceCharge.BdServiceCharge;
import com.hzs.common.domain.serviceCharge.BdServiceChargeDetail;
import com.hzs.common.security.utils.SecurityUtils;

import com.hzs.system.config.dto.ServiceChargeDTO;
import com.hzs.system.config.mapper.BdServiceChargeMapper;
import com.hzs.system.config.service.IBdServiceChargeDetailService;
import com.hzs.system.config.service.IBdServiceChargeService;
import com.hzs.system.config.vo.ServiceChargeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 办理业务手续费配置主表 服务实现类
 */
@Service
public class BdServiceChargeServiceImpl extends ServiceImpl<BdServiceChargeMapper, BdServiceCharge> implements IBdServiceChargeService {

    @Autowired
    private IBdServiceChargeService iBdServiceChargeService;
    @Autowired
    private IBdServiceChargeDetailService iBdServiceChargeDetailService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveServiceCharge(ServiceChargeVo serviceChargeVo) {
        BdServiceCharge bdServiceCharge = BeanUtil.copyProperties(serviceChargeVo, BdServiceCharge.class);
        bdServiceCharge.setPkCreator(SecurityUtils.getUserId());
        bdServiceCharge.setCreationTime(new Date());
        bdServiceCharge.setPkCountry(serviceChargeVo.getPkCountry());
        iBdServiceChargeService.save(bdServiceCharge);
        for (BdServiceChargeDetail bdServiceChargeDetail : serviceChargeVo.getServiceChargeDetailsList()) {
            bdServiceChargeDetail.setPkServiceCharge(bdServiceCharge.getPkId());
            bdServiceChargeDetail.setPkCountry(serviceChargeVo.getPkCountry());
        }
        iBdServiceChargeDetailService.saveBatch(serviceChargeVo.getServiceChargeDetailsList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateServiceCharge(ServiceChargeVo serviceChargeVo) {
        BdServiceCharge bdServiceCharge = BeanUtil.copyProperties(serviceChargeVo, BdServiceCharge.class);
        bdServiceCharge.setPkModified(SecurityUtils.getUserId());
        bdServiceCharge.setModifiedTime(new Date());
        iBdServiceChargeService.updateById(bdServiceCharge);
        UpdateWrapper<BdServiceChargeDetail> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_SERVICE_CHARGE", bdServiceCharge.getPkId());
        iBdServiceChargeDetailService.remove(updateWrapper);
        for (BdServiceChargeDetail bdServiceChargeDetail : serviceChargeVo.getServiceChargeDetailsList()) {
            bdServiceChargeDetail.setPkServiceCharge(bdServiceCharge.getPkId());
        }
        iBdServiceChargeDetailService.saveBatch(serviceChargeVo.getServiceChargeDetailsList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteServiceCharge(Long pkServiceCharge) {
        iBdServiceChargeService.removeById(pkServiceCharge);
        UpdateWrapper<BdServiceChargeDetail> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_SERVICE_CHARGE", pkServiceCharge);
        iBdServiceChargeDetailService.remove(updateWrapper);
    }

    @Override
    public ServiceChargeVo getServiceCharge(Long pkServiceCharge) {
        BdServiceCharge serviceCharge = iBdServiceChargeService.getById(pkServiceCharge);
        ServiceChargeVo serviceChargeVo = BeanUtil.copyProperties(serviceCharge, ServiceChargeVo.class);
        serviceChargeVo.setPkServiceCharge(serviceCharge.getPkId());
        QueryWrapper<BdServiceChargeDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_SERVICE_CHARGE", serviceCharge.getPkId());
        List<BdServiceChargeDetail> serviceChargeDetailsList = iBdServiceChargeDetailService.list(queryWrapper);
        serviceChargeVo.setServiceChargeDetailsList(serviceChargeDetailsList);
        return serviceChargeVo;
    }

    @Override
    public ServiceChargeDTO getServiceChargeByType(ServiceChargeDTO serviceChargeDTO) {
        return baseMapper.getServiceChargeByType(serviceChargeDTO);
    }

    @Override
    public List<ServiceChargeVo> selectByList(BdServiceCharge serviceCharge) {
        return baseMapper.selectByList(serviceCharge);
    }

}
