package com.xy.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.xy.annotate.RestMappingController;
import com.xy.dto.DeviceSimChargeDto;
import com.xy.dto.DeviceSimDto;
import com.xy.service.DeviceSimChargeServiceImpl;
import com.xy.service.DeviceSimServiceImpl;
import com.xy.util.ExcelUtils;
import com.xy.utils.MercAuthUtils;
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 javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@Service
@AllArgsConstructor
@Api(tags = "小程序-设备流量卡充值表")
@RestMappingController("merc-mini/deviceSimCharge")
public class DeviceSimChargeController {

    private DeviceSimChargeServiceImpl deviceSimChargeService;
    private DeviceSimServiceImpl deviceSimService;

    @PostMapping("page")
    @ApiOperation("分页查询")
    public R<PageBean<DeviceSimChargeDto.Vo>> page(@RequestBody DeviceSimChargeDto.Page page) {
        page.setMercId(MercAuthUtils.getMercId());
        return deviceSimChargeService.page(page);
    }

    @ApiOperation("商管端-月统计分页")
    @PostMapping("mercMoonCountPage")
    public R<PageBean<DeviceSimChargeDto.MoonCountVo>> mercMoonCountPage(@RequestBody DeviceSimChargeDto.MoonCountPage moonCountPage) {
        moonCountPage.setMercIds(CollUtil.newArrayList(MercAuthUtils.getMercId()));
        return deviceSimChargeService.moonCountPage(moonCountPage);
    }

    @PostMapping("deviceSimDetailPage")
    @ApiOperation("商管端-流量卡明细")
    public R<PageBean<DeviceSimDto.PageVo>> deviceSimDetailPage(@RequestBody @Validated DeviceSimDto.Page page) {
        page.setMercId(MercAuthUtils.getMercId());
        return deviceSimService.page(page);
    }

    @PostMapping("mySpendPage")
    @ApiOperation("财务管理-支出管理-流量卡费明细")
    public R<PageBean<DeviceSimChargeDto.MySpendPageVO>> mySpendPage(@RequestBody @Validated DeviceSimChargeDto.PageByTopMerc page) {
        page.setCurMercId(MercAuthUtils.getMercId());
        return deviceSimChargeService.mySpendPage(page);
    }


    @ApiOperation("财务管理-支出管理-流量卡费明细")
    @PostMapping("exportMySpendPage")
    public void exportMySpendPage(HttpServletResponse response, @RequestBody @Valid DeviceSimChargeDto.PageByTopMerc page) throws IOException {
        page.setCurMercId(MercAuthUtils.getMercId());
        R<PageBean<DeviceSimChargeDto.MySpendPageVO>> data = deviceSimChargeService.mySpendPage(page);
        List<DeviceSimChargeDto.MySpendPageVO> records = data.getData().getRecords();
        if (CollUtil.isNotEmpty(records)) {
            for (DeviceSimChargeDto.MySpendPageVO record : records) {
                Integer status = record.getStatus();
                if (status.intValue() != 2) {
                    record.setCreateTime(null);
                }
            }
        }
        List<DeviceSimChargeDto.SpendPageExcelVo> deviceExcelVOS = BeanUtil.copyToList(records, DeviceSimChargeDto.SpendPageExcelVo.class);
        // 输出
        ExcelUtils.write(response, "流量卡费用明细.xls", "流量卡费用明细", DeviceSimChargeDto.SpendPageExcelVo.class, deviceExcelVOS);
    }

    @PostMapping("mySpendPageCount")
    @ApiOperation("财务管理-支出管理-流量卡费统计")
    public R<Integer> mySpendPageCount(@RequestBody @Validated DeviceSimChargeDto.PageByTopMerc page) {
        page.getPage().setSize(-1);
        page.setCurMercId(MercAuthUtils.getMercId());
        PageBean<DeviceSimChargeDto.MySpendPageVO> pageVoPageBean = R.feignCheckData(deviceSimChargeService.mySpendPage(page));
        List<DeviceSimChargeDto.MySpendPageVO> records = pageVoPageBean.getRecords();
        int totaMoney = 0;
        if (CollUtil.isNotEmpty(records)) {
            for (DeviceSimChargeDto.MySpendPageVO r : records) {
                totaMoney += r.getMoney();
            }
        }
        return R.ok(totaMoney);
    }


    @PostMapping("pageByTopMerc")
    @ApiOperation("加盟商-服务费管理-流量卡费明细")
    public R<PageBean<DeviceSimChargeDto.PageByTopMercVO>> pageByTopMerc(@RequestBody @Validated DeviceSimChargeDto.PageByTopMerc page) {
        page.setCurMercId(MercAuthUtils.getMercId());
        return deviceSimChargeService.pageByTopMerc(page);
    }

    @PostMapping("pageByTopMercCount")
    @ApiOperation("加盟商-服务费管理-流量卡费佣金统计")
    public R<Integer> pageByTopMercCount(@RequestBody @Validated DeviceSimChargeDto.PageByTopMerc page) {
        page.getPage().setSize(-1);
        page.setCurMercId(MercAuthUtils.getMercId());
        PageBean<DeviceSimChargeDto.PageByTopMercVO> pageVoPageBean = R.feignCheckData(deviceSimChargeService.pageByTopMerc(page));
        if (pageVoPageBean == null) {
            return R.ok(0);
        }
        List<DeviceSimChargeDto.PageByTopMercVO> records = pageVoPageBean.getRecords();
        int totalAgentMoney = 0;
        if (CollUtil.isNotEmpty(records)) {
            for (DeviceSimChargeDto.PageByTopMercVO r : records) {
                totalAgentMoney += r.getAgentMoney();
            }
        }
        return R.ok(totalAgentMoney);
    }


    @ApiOperation("加盟商-服务费管理-流量卡明细导出")
    @PostMapping("exportPageByTopMerc")
    public void exportPageByTopMerc(HttpServletResponse response, @RequestBody @Valid DeviceSimChargeDto.PageByTopMerc page) throws IOException {
        page.setCurMercId(MercAuthUtils.getMercId());
        R<PageBean<DeviceSimChargeDto.PageByTopMercVO>> data = deviceSimChargeService.pageByTopMerc(page);
        List<DeviceSimChargeDto.PageByTopMercVO> records = data.getData().getRecords();
        if (CollUtil.isNotEmpty(records)) {
            for (DeviceSimChargeDto.PageByTopMercVO record : records) {
                Integer status = record.getStatus();
                if (status.intValue() != 2) {
                    record.setCreateTime(null);
                }
            }
        }
        List<DeviceSimChargeDto.PageExcelVo> deviceExcelVOS = BeanUtil.copyToList(records, DeviceSimChargeDto.PageExcelVo.class);
        // 输出
        ExcelUtils.write(response, "流量卡费用明细.xls", "流量卡费用明细", DeviceSimChargeDto.PageExcelVo.class, deviceExcelVOS);
    }

}
