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.DeviceAlgorithmChargingHistoryDto;
import com.xy.service.DeviceAlgorithmChargingHistoryServiceImpl;
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/deviceAlgorithmChargingHis")
public class DeviceAlgorithmChargingHisController {

    private DeviceAlgorithmChargingHistoryServiceImpl deviceAlgorithmChargingHistoryService;


    @PostMapping("mySpendPage")
    @ApiOperation("财务管理-支出管理-设备激活费明细")
    public R<PageBean<DeviceAlgorithmChargingHistoryDto.MySpendPageVO>> mySpendPage(@RequestBody @Validated DeviceAlgorithmChargingHistoryDto.PageByTopMerc page) {
        page.setCurMercId(MercAuthUtils.getMercId());
        return deviceAlgorithmChargingHistoryService.mySpendPage(page);
    }

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

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


    @PostMapping("pageByTopMerc")
    @ApiOperation("加盟商-服务费管理-设备激活费明细")
    public R<PageBean<DeviceAlgorithmChargingHistoryDto.PageByTopMercVO>> pageByTopMerc(@RequestBody @Validated DeviceAlgorithmChargingHistoryDto.PageByTopMerc page) {
        page.setCurMercId(MercAuthUtils.getMercId());
        return deviceAlgorithmChargingHistoryService.pageByTopMerc(page);
    }

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


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

}
