package cn.pluss.platform.controller;

import cn.jiguang.common.utils.StringUtils;
import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.dto.LeshuaBillsDTO;
import cn.pluss.platform.entity.MakeMoney;
import cn.pluss.platform.entity.MerchantIncome;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.merchantIncome.MerchantIncomeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/merchantIncome")
@Api(value = "MerchantIncome控制类", tags = {"MerchantIncome接口"})
public class MerchantIncomeController {

    @Resource
    private MerchantIncomeService merchantIncomeService;

    @GetMapping
    @ApiOperation(tags = {"页面-余额"}, value = "根据日期获取对应商家指定时间段内的收益", notes = "返回分页过后的数据", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "查询页码", paramType = "query", dataType = "Integer", defaultValue = "0"),
            @ApiImplicitParam(name = "size", value = "每页数据量", paramType = "query", dataType = "Integer", defaultValue = "0"),
            @ApiImplicitParam(name = "merchantCode", value = "商家的编码信息", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "startTime", value = "查询的开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "查询的结束时间", paramType = "query", dataType = "String")})
    public Result<Map<String, Object>> summary(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size, String merchantCode, @RequestParam String startTime, @RequestParam String endTime) throws ParseException {
        Map<String, Object> resultMap = merchantIncomeService.queryTotalEnterFee(merchantCode, startTime, endTime);
        return ResultGenerator.genSuccessResult(resultMap);
    }

    @PostMapping
    @ApiOperation(value = "添加数据", notes = "添加新的数据", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "MerchantIncome", value = "待添加的MerchantIncome实例", paramType = "body", dataType = "MerchantIncome", required = true)})
    public Result add(@RequestBody MerchantIncome merchantIncome) {
        merchantIncomeService.saveMerchantIncome(merchantIncome);
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除数据", notes = "根据id删除数据", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "查询的id实例", paramType = "path", dataType = "Integer", required = true)})
    public Result delete(@PathVariable Integer id) {
        MerchantIncome merchantIncome = new MerchantIncome();
        merchantIncome.setId(id);
        merchantIncomeService.deleteMerchantIncome(merchantIncome);
        return ResultGenerator.genSuccessResult();
    }


    @GetMapping("/{dealCode}")
    @ApiOperation(value = "获取单个收益的详情", notes = "查看单个项目的内容", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dealCode", value = "查询的dealCode", paramType = "path", required = true, dataType = "String")})
    public Result detail(@PathVariable String dealCode) {
        MerchantIncome merchantIncome = new MerchantIncome();
        merchantIncome.setDealCode(dealCode);
        merchantIncome = merchantIncomeService.queryMerchantIncome(merchantIncome);
        return ResultGenerator.genSuccessResult(merchantIncome);
    }

    @PostMapping("/queryLastFiveIncome")
    @ApiOperation(value = "查询商家最后的五条收入记录", notes = "查询最后的五条收入记录", httpMethod = "GET")
    public Result<List<MerchantIncome>> queryLastFiveIncome(@RequestBody UserApp userApp) {
        return ResultGenerator.genSuccessResult(merchantIncomeService.queryLastFiveMerchantIncome2(userApp));
    }

    @PostMapping("/queryLeshuaBills")
    @ApiOperation(tags = {"页面-余额"}, value = "查询乐刷打款账单", notes = "查询乐刷打款账单", httpMethod = "POST")
    public Result<PageInfo<MakeMoney>> queryLeshuaBills(@RequestBody LeshuaBillsDTO leshuaBillsDTO) throws ParseException {
        if (StringUtils.isEmpty(leshuaBillsDTO.getMerchantId())) {
            return ResultGenerator.genSuccessResult(new PageInfo<>(0L, 0L, 0L, 20L, new ArrayList<>()));
        }

        PageInfo<MakeMoney> result = merchantIncomeService.queryLeshuaBills(leshuaBillsDTO);
        return ResultGenerator.genSuccessResult(result);
    }
}
