package com.xy.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.device.EnumSimConfig;
import com.xy.dto.MercFeeConfigAlgorithmTypeDto;
import com.xy.dto.MercFeeConfigDto;
import com.xy.dto.MercFeeConfigSimCardDto;
import com.xy.dto.be.MercDto;
import com.xy.entity.MercFeeConfigSimCard;
import com.xy.error.CommRuntimeException;
import com.xy.mapper.MercFeeConfigSimCardMapper;
import com.xy.service.be.MercService;
import com.xy.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

import static com.xy.utils.Beans.copy;
import static com.xy.utils.PlusBeans.toIPage;
import static com.xy.utils.PlusBeans.toPageBean;


/**
 * <p>
 * 商户流量卡费配置扩展 服务实现类
 * </p>
 *
 * @author 谭斌
 * @since 2024-04-07
 */
@Service
@AllArgsConstructor
@Api(tags = "商户流量卡费配置扩展")
public class MercFeeConfigSimCardServiceImpl extends ServiceImpl<MercFeeConfigSimCardMapper, MercFeeConfigSimCard> implements MercFeeConfigSimCardService {

    private MercService mercService;
    private DeviceAlgorithmChargingServiceImpl deviceAlgorithmChargingService;
    private MercFeeConfigService mercFeeConfigService;


    @PostMapping("obj")
    @ApiOperation("对象查询")
    public R<MercFeeConfigSimCardDto.Vo> obj(@RequestBody MercFeeConfigSimCardDto.Vo vo) {
        MercFeeConfigSimCardDto.SelectList selectList = copy(MercFeeConfigSimCardDto.SelectList.class, vo);
        List<MercFeeConfigSimCardDto.Vo> list = list(selectList).getData();
        if (Emptys.check(list)) {
            return R.ok(list.get(0));
        }
        return R.ok();
    }

    @PostMapping("list")
    @ApiOperation("集合查询")
    public R<List<MercFeeConfigSimCardDto.Vo>> list(@RequestBody MercFeeConfigSimCardDto.SelectList selectList) {
        LambdaQueryWrapper<MercFeeConfigSimCard> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(selectList, MercFeeConfigSimCard.class).build();
        List<MercFeeConfigSimCard> list = list(lambdaQueryWrapper);
        return R.ok(copy(MercFeeConfigSimCardDto.Vo.class, list));
    }

    @PostMapping("page")
    @ApiOperation("分页查询")
    public R<PageBean<MercFeeConfigSimCardDto.Vo>> page(@RequestBody MercFeeConfigSimCardDto.Page page) {
        PageBean pageBean = page.getPage();
        LambdaQueryWrapper<MercFeeConfigSimCard> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(page, MercFeeConfigSimCard.class).build();
        IPage<MercFeeConfigSimCard> iPage = page(toIPage(pageBean), lambdaQueryWrapper);
        return R.ok(toPageBean(MercFeeConfigSimCardDto.Vo.class, iPage));
    }

    @PostMapping("listByFeeConfig")
    @ApiOperation("流量卡费用配置查询")
    public R<MercFeeConfigSimCardDto.DeviceSimConfigVO> listByFeeConfig(@RequestBody @Validated MercFeeConfigAlgorithmTypeDto.ListByFeeConfigDTO dto) {
        MercFeeConfigSimCardDto.DeviceSimConfigVO data = new MercFeeConfigSimCardDto.DeviceSimConfigVO();
        Long mercId = dto.getMercId();
        MercDto.Vo merc = R.feignCheckData(mercService.obj(new MercDto.Vo().setId(mercId)));
        //查询默认算法信息
        //流量卡费-平台默认配置
        List<MercFeeConfigSimCardDto.DeviceSimPlatformVO> deviceSimPlatformList = new ArrayList<>();

        Integer money = SysDictUtils.getValue(EnumSimConfig.Code.CODE.getCode(), EnumSimConfig.money.getCode(), Integer.class);
        String name = SysDictUtils.getValue(EnumSimConfig.Code.CODE.getCode(), EnumSimConfig.name.getCode(), String.class);
        Integer expireDay = SysDictUtils.getValue(EnumSimConfig.Code.CODE.getCode(), EnumSimConfig.N_200.getCode(), Integer.class);
        deviceSimPlatformList.add(new MercFeeConfigSimCardDto.DeviceSimPlatformVO().setName(name).setExpireDay(expireDay).setMoney(money));


        // 设备管理费-商家自定义费用
        List<MercFeeConfigSimCardDto.DeviceSimMercVO> deviceSimMercList = new ArrayList<>();
        LambdaQueryWrapper<MercFeeConfigSimCard> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(dto, MercFeeConfigSimCard.class)
                .eq(MercFeeConfigSimCard::getMercId, merc.getId())
                .build();
        //默认配置 type 2预充 1赠送天数
        List<MercFeeConfigSimCard> list = list(lambdaQueryWrapper);
        if (CollUtil.isNotEmpty(list)) {
            for (MercFeeConfigSimCard cfg : list) {
                MercFeeConfigSimCardDto.DeviceSimMercVO deviceSimMercVO = BeanUtil.copyProperties(cfg, MercFeeConfigSimCardDto.DeviceSimMercVO.class);
                deviceSimMercVO.setName(name).setExpireDay(expireDay).setMoney(money);
                deviceSimMercList.add(deviceSimMercVO);
            }
        }
        data.setDeviceSimPlatformList(deviceSimPlatformList);
        data.setDeviceSimMercList(deviceSimMercList);
        return R.ok(data);
    }

    @PostMapping("paySimCardFee")
    @ApiOperation("流量卡费用")
    public R<List<MercFeeConfigSimCardDto.DeviceSimPayVO>> paySimCardFee(@RequestBody @Validated MercFeeConfigAlgorithmTypeDto.SimPayQueryDTO dto) {
        Long mercId = dto.getMercId();
        MercDto.Vo merc = R.feignCheckData(mercService.obj(new MercDto.Vo().setId(mercId)));
        String mercCode = merc.getMercCode();
        List<String> mercCodes = new ArrayList<>();
        deviceAlgorithmChargingService.getMercCodes(mercCode, mercCodes);
        //查询默认算法信息
        //流量卡费-平台默认配置
        Integer money = SysDictUtils.getValue(EnumSimConfig.Code.CODE.getCode(), EnumSimConfig.money.getCode(), Integer.class);
        String name = SysDictUtils.getValue(EnumSimConfig.Code.CODE.getCode(), EnumSimConfig.name.getCode(), String.class);
        //佣金配置查询
        MercFeeConfigDto.FeeConfigByMercVO mercFeeConfig = R.feignCheckData(mercFeeConfigService.getFeeConfigByMerc(new MercFeeConfigDto.FeeConfigByMerc().setMercCode(merc.getMercCode())));

        // 设备管理费-商家自定义费用
        List<MercFeeConfigSimCardDto.DeviceSimPayVO> deviceSimMercList = new ArrayList<>();
        LambdaQueryWrapper<MercFeeConfigSimCard> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(dto, MercFeeConfigSimCard.class)
                .in(MercFeeConfigSimCard::getMercCode, mercCodes)
                .build();
        List<MercFeeConfigSimCard> list = list(lambdaQueryWrapper);
        if (CollUtil.isNotEmpty(list)) {
            for (MercFeeConfigSimCard cfg : list) {
                MercFeeConfigSimCardDto.DeviceSimPayVO deviceSimMercVO = BeanUtil.copyProperties(cfg, MercFeeConfigSimCardDto.DeviceSimPayVO.class);
                deviceSimMercVO.setName(name).setMoney(cfg.getExtraMoney() + mercFeeConfig.getFeeSimcard());
                deviceSimMercList.add(deviceSimMercVO);
            }
        } else {
            MercFeeConfigSimCardDto.DeviceSimPayVO deviceSimMercVO = new MercFeeConfigSimCardDto.DeviceSimPayVO();
            deviceSimMercVO.setName(name).setMoney(money + mercFeeConfig.getFeeSimcard());
            deviceSimMercList.add(deviceSimMercVO);
        }

        return R.ok(deviceSimMercList);
    }

    @Override
    @ApiOperation("商户自定义流量卡费用")
    public R<List<MercFeeConfigSimCardDto.DeviceSimPayVO>> listByMerc(MercFeeConfigAlgorithmTypeDto.SimPayQueryDTO dto) {
        Long mercId = dto.getMercId();
        MercDto.Vo merc = R.feignCheckData(mercService.obj(new MercDto.Vo().setId(mercId)));
        String mercCode = merc.getMercCode();
        List<String> mercCodes = new ArrayList<>();
        deviceAlgorithmChargingService.getMercCodes(mercCode, mercCodes);
        //查询默认算法信息
        //流量卡费-平台默认配置
        Integer money = SysDictUtils.getValue(EnumSimConfig.Code.CODE.getCode(), EnumSimConfig.money.getCode(), Integer.class);
        String name = SysDictUtils.getValue(EnumSimConfig.Code.CODE.getCode(), EnumSimConfig.name.getCode(), String.class);

        // 设备管理费-商家自定义费用
        List<MercFeeConfigSimCardDto.DeviceSimPayVO> deviceSimMercList = new ArrayList<>();
        LambdaQueryWrapper<MercFeeConfigSimCard> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(dto, MercFeeConfigSimCard.class)
                .in(MercFeeConfigSimCard::getMercCode, mercCodes)
                .build();
        List<MercFeeConfigSimCard> list = list(lambdaQueryWrapper);
        if (CollUtil.isNotEmpty(list)) {
            for (MercFeeConfigSimCard cfg : list) {
                MercFeeConfigSimCardDto.DeviceSimPayVO deviceSimMercVO = BeanUtil.copyProperties(cfg, MercFeeConfigSimCardDto.DeviceSimPayVO.class);
                deviceSimMercVO.setName(name).setMoney(money + cfg.getExtraMoney());
                deviceSimMercList.add(deviceSimMercVO);
            }
        }

        return R.ok(deviceSimMercList);
    }


    @PostMapping("save")
    @ApiOperation("添加")
    public R save(@RequestBody @Validated MercFeeConfigSimCardDto.Save save) {
        Long mercId = save.getMercId();
        MercFeeConfigSimCard mc = this.getOne(Wrappers.<MercFeeConfigSimCard>lambdaQuery()
                .eq(MercFeeConfigSimCard::getMercId, mercId)
        );
        if (mc != null) {
            throw new CommRuntimeException("该类型配置已存在，请勿重复添加！");
        }
        MercFeeConfigSimCard saveInfo = copy(MercFeeConfigSimCard.class, save);
        save(saveInfo);
        return R.ok();
    }

    @PostMapping("update")
    @ApiOperation("修改")
    public R update(@RequestBody @Validated MercFeeConfigSimCardDto.Update update) {
        MercFeeConfigSimCard updateInfo = copy(MercFeeConfigSimCard.class, update);
        updateById(updateInfo);
        return R.ok();
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    public R delete(@RequestBody @Validated MercFeeConfigSimCardDto.Delete delete) {
        removeById(delete.getId());
        return R.ok();
    }
}
