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.core.constant.CacheConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.service.RedisService;
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;

/**
 * <p>
 * 办理业务手续费配置主表 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2023-01-03
 */
@Service
public class BdServiceChargeServiceImpl extends ServiceImpl<BdServiceChargeMapper, BdServiceCharge> implements IBdServiceChargeService {
    @Autowired
    private IBdServiceChargeService serviceChargeService;
    @Autowired
    private IBdServiceChargeDetailService serviceChargeDetailService;
    @Autowired
    private RedisService redisService;

    /**
     * 新增
     *
     * @param serviceChargeVo
     * @return
     */
    @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());
        serviceChargeService.save(bdServiceCharge);
        for (BdServiceChargeDetail bdServiceChargeDetail : serviceChargeVo.getServiceChargeDetailsList()) {
            bdServiceChargeDetail.setPkServiceCharge(bdServiceCharge.getPkId());
            bdServiceChargeDetail.setPkCountry(serviceChargeVo.getPkCountry());
        }
        serviceChargeDetailService.saveBatch(serviceChargeVo.getServiceChargeDetailsList());
    }

    /**
     * 修改
     *
     * @param serviceChargeVo
     * @return
     */
    @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());
        serviceChargeService.updateById(bdServiceCharge);
        UpdateWrapper<BdServiceChargeDetail> updateWrapper =new UpdateWrapper<>();
        updateWrapper.eq("PK_SERVICE_CHARGE",bdServiceCharge.getPkId());
        serviceChargeDetailService.remove(updateWrapper);
        for (BdServiceChargeDetail bdServiceChargeDetail : serviceChargeVo.getServiceChargeDetailsList()) {
            bdServiceChargeDetail.setPkServiceCharge(bdServiceCharge.getPkId());
        }
        serviceChargeDetailService.saveBatch(serviceChargeVo.getServiceChargeDetailsList());
    }

    /**
     * 删除
     *
     * @param pkServiceCharge
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteServiceCharge(Long pkServiceCharge) {

        serviceChargeService.removeById(pkServiceCharge);
        UpdateWrapper<BdServiceChargeDetail> updateWrapper =new UpdateWrapper<>();
        updateWrapper.eq("PK_SERVICE_CHARGE",pkServiceCharge);
        serviceChargeDetailService.remove(updateWrapper);
    }

    /**
     * 查询详情
     *
     * @param pkServiceCharge
     * @return
     */
    @Override
    public ServiceChargeVo getServiceCharge(Long pkServiceCharge) {
    //    String redisKey = CacheConstants.BD_AREA_CLASSIFY + pkServiceCharge;
   //     ServiceChargeVo sv = redisService.getCacheObject(redisKey);
        //if (sv == null) {
            BdServiceCharge serviceCharge = serviceChargeService.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 = serviceChargeDetailService.list(queryWrapper);
            serviceChargeVo.setServiceChargeDetailsList(serviceChargeDetailsList);
    //        sv = serviceChargeVo;
     //   }
        return serviceChargeVo;
    }

    /**
     * @description: 根据业务类型查询扣费
     * @author: zhang jing
     * @date: 2023/1/7 14:50
     * @param: [serviceChargeVo]
     * @return: com.hzs.system.config.vo.ServiceChargeVo
     **/
    @Override
    public ServiceChargeDTO getServiceChargeByType(ServiceChargeDTO serviceChargeDTO) {
        return baseMapper.getServiceChargeByType(serviceChargeDTO);
    }

    /**
     * 查询列表
     * @param serviceCharge
     * @return
     */
    @Override
    public List<ServiceChargeVo> selectByList(BdServiceCharge serviceCharge) {
        return baseMapper.selectByList(serviceCharge);
    }

}
