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.extension.service.impl.ServiceImpl;
import com.xy.dto.MercFeeSettleDto;
import com.xy.dto.MercFeeStatisticDto;
import com.xy.entity.MercFeeStatistic;
import com.xy.mapper.MercFeeStatisticMapper;
import com.xy.utils.Emptys;
import com.xy.utils.MybatisPlusQuery;
import com.xy.utils.PageBean;
import com.xy.utils.R;
import com.xy.utils.enums.MercFeeSettleSate;
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.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-24
 */
@Service
@AllArgsConstructor
@Api(tags = "商户佣金费用统计")
public class MercFeeStatisticServiceImpl extends ServiceImpl<MercFeeStatisticMapper, MercFeeStatistic> implements MercFeeStatisticService {

    private MercFeeSettleServiceImpl mercFeeSettleService;

    /**
     * 收益总览
     *
     * @param mercId
     * @return
     */
    public R<MercFeeStatisticDto.InFoVO> info(Long mercId) {
        MercFeeStatistic mercFeeStatistic = getByMercId(mercId);
        MercFeeStatisticDto.InFoVO inFoVO = BeanUtil.copyProperties(mercFeeStatistic, MercFeeStatisticDto.InFoVO.class);
        //结算中金额
        int settlingMoney = 0;
        MercFeeSettleDto.SelectList selectList = new MercFeeSettleDto.SelectList();
        selectList.setMercId(mercId);
        selectList.setSettleState(MercFeeSettleSate.S2.getCode());
        List<MercFeeSettleDto.Vo> list = R.feignCheckData(mercFeeSettleService.list(selectList));
        if (CollUtil.isNotEmpty(list)) {
            settlingMoney = list.stream().mapToInt(MercFeeSettleDto.Vo::getSettleMoney).sum();
        }
        inFoVO.setSettlingMoney(settlingMoney);
        return R.ok(inFoVO);
    }


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

    /**
     * 获取商户统计数据，不存在则初始化新增
     *
     * @param mercId
     * @return
     */
    public MercFeeStatistic getByMercId(Long mercId) {
        MercFeeStatistic mercFeeStatistic = this.getById(mercId);
        if (mercFeeStatistic == null) {
            MercFeeStatistic newObj = new MercFeeStatistic()
                    .setMercId(mercId)
                    .setSettleMoney(0)
                    .setUnSettleMoney(0)
                    .setVerifyMoney(0);
            save(newObj);
            mercFeeStatistic = newObj;
        }
        return mercFeeStatistic;
    }

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

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

    @PostMapping("save")
    @ApiOperation("添加")
    public R save(@RequestBody @Validated MercFeeStatisticDto.Save save) {
        MercFeeStatistic saveInfo = copy(MercFeeStatistic.class, save);
        save(saveInfo);
        return R.ok();
    }

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