package com.xy.service;

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.MercFeeCountDayDto;
import com.xy.entity.MercFeeCountDay;
import com.xy.mapper.MercFeeCountDayMapper;
import com.xy.utils.Emptys;
import com.xy.utils.MybatisPlusQuery;
import com.xy.utils.PageBean;
import com.xy.utils.R;
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-23
 */
@Service
@AllArgsConstructor
@Api(tags = "商户佣金费用收益每日统计（不含今天）")
public class MercFeeCountDayServiceImpl extends ServiceImpl<MercFeeCountDayMapper, MercFeeCountDay> implements MercFeeCountDayService {

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

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

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

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

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

    /**
     * 查询指定月份费用总和
     *
     * @param countByMonth
     * @return
     */
    public List<MercFeeCountDayDto.CountByMonthVO> countByMonth(MercFeeCountDayDto.CountByMonth countByMonth) {
        return this.baseMapper.countByMonth(countByMonth);
    }
}
