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.DeviceChargingHistoryDto;
import com.xy.service.DeviceChargingHistoryServiceImpl;
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.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;

@AllArgsConstructor
@Api(tags = "小程序-设备计费历史表")
@RestMappingController("merc-mini/device-charging-history")
public class DeviceChargingHistoryController {

    private DeviceChargingHistoryServiceImpl deviceChargingHistoryService;

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

    @PostMapping("mySpendPage")
    @ApiOperation("财务管理-支出管理-设备管理费")
    public R<PageBean<DeviceChargingHistoryDto.MySpendPageVO>> mySpendPage(@RequestBody DeviceChargingHistoryDto.PageByTopMerc page) {
        page.setCurMercId(MercAuthUtils.getMercId());
        return deviceChargingHistoryService.mySpendPage(page);
    }

    @ApiOperation("财务管理-支出管理-设备管理费导出")
    @PostMapping("exportMySpendPage")
    public void exportMySpendPage(HttpServletResponse response, @RequestBody @Valid DeviceChargingHistoryDto.PageByTopMerc page) throws IOException {
        page.setCurMercId(MercAuthUtils.getMercId());
        R<PageBean<DeviceChargingHistoryDto.MySpendPageVO>> data = deviceChargingHistoryService.mySpendPage(page);
        List<DeviceChargingHistoryDto.MySpendPageVO> records = data.getData().getRecords();
        List<DeviceChargingHistoryDto.SpendPageExcelVo> deviceExcelVOS = BeanUtil.copyToList(records, DeviceChargingHistoryDto.SpendPageExcelVo.class);
        // 输出
        ExcelUtils.write(response, "设备管理费缴费记录明细.xls", "设备管理费缴费记录明细", DeviceChargingHistoryDto.SpendPageExcelVo.class, deviceExcelVOS);
    }


    @PostMapping("mySpendPageCount")
    @ApiOperation("财务管理-支出管理-设备管理费缴费统计")
    public R<Integer> mySpendPageCount(@RequestBody DeviceChargingHistoryDto.PageByTopMerc page) {
        page.getPage().setSize(-1);
        page.setCurMercId(MercAuthUtils.getMercId());
        PageBean<DeviceChargingHistoryDto.MySpendPageVO> pageVoPageBean = R.feignCheckData(deviceChargingHistoryService.mySpendPage(page));
        List<DeviceChargingHistoryDto.MySpendPageVO> records = pageVoPageBean.getRecords();
        int totalMoney = 0;
        if (CollUtil.isNotEmpty(records)) {
            for (DeviceChargingHistoryDto.MySpendPageVO r : records) {
                totalMoney += r.getChargingMoney();
            }
        }
        return R.ok(totalMoney);
    }


    @PostMapping("pageByTopMerc")
    @ApiOperation("加盟商-服务费管理-设备管理费")
    public R<PageBean<DeviceChargingHistoryDto.PageVo>> pageByTopMerc(@RequestBody @Validated DeviceChargingHistoryDto.PageByTopMerc page) {
        page.setCurMercId(MercAuthUtils.getMercId());
        return deviceChargingHistoryService.pageByTopMerc(page);
    }

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


    @ApiOperation("加盟商-服务费管理-设备管理费明细")
    @PostMapping("exportPageByTopMerc")
    public void exportPageByTopMerc(HttpServletResponse response, @RequestBody @Valid DeviceChargingHistoryDto.PageByTopMerc page) throws IOException {
        page.setCurMercId(MercAuthUtils.getMercId());
        R<PageBean<DeviceChargingHistoryDto.PageVo>> data = deviceChargingHistoryService.pageByTopMerc(page);
        List<DeviceChargingHistoryDto.PageVo> records = data.getData().getRecords();
        List<DeviceChargingHistoryDto.PageExcelVo> deviceExcelVOS = BeanUtil.copyToList(records, DeviceChargingHistoryDto.PageExcelVo.class);
        // 输出
        ExcelUtils.write(response, "设备管理费明细.xls", "设备管理费明细", DeviceChargingHistoryDto.PageExcelVo.class, deviceExcelVOS);
    }
}
