package shop.alien.store.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreIncomeDetailsRecord;
import shop.alien.entity.store.vo.StoreIncomeDetailsRecordVo;
import shop.alien.store.service.StoreIncomeDetailsRecordService;


import java.util.Map;

/**
 * 商户收入明细表 前端控制器
 *
 * @author ssk
 * @since 2025-02-25
 */
@Slf4j
@Api(tags = {"商户收入明细"})
@ApiSort(16)
@CrossOrigin
@RestController
@RequestMapping("/storeIncomeDetailsRecord")
@RequiredArgsConstructor
public class StoreIncomeDetailsRecordController {

    private final StoreIncomeDetailsRecordService storeIncomeDetailsRecordService;


    @ApiOperation("提现-提现全部/部分提现")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "payPassword", value = "支付密码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "withdrawalMoney", value = "提现金额", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/cashOut")
    @Deprecated
    public R cashOut(Integer storeId, String payPassword, Integer withdrawalMoney) {
        return storeIncomeDetailsRecordService.cashOut(storeId, payPassword, withdrawalMoney);
    }

    @ApiOperation("提现申请-提现全部/部分提现")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "payPassword", value = "支付密码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "withdrawalMoney", value = "提现金额", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/applyCashOut")
    public R applyCashOut(Integer storeId, String payPassword, Integer withdrawalMoney) {
           return storeIncomeDetailsRecordService.applyCashOut(storeId, payPassword, withdrawalMoney);
    }

    @ApiOperation("提现审批-提现全部/部分提现")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "cashOutId", value = "提现申请记录ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "approveStatus", value = "审批状态", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "failReason", value = "失败原因", dataType = "String", paramType = "query", required = false)})
    @GetMapping("/approveCashOut")
    public R approveCashOut(Integer cashOutId, String approveStatus, String failReason) {
        String s = storeIncomeDetailsRecordService.approveCashOut(cashOutId, approveStatus, failReason);
        if ("付款失败".equals(s)) {
            return R.fail("付款失败");
        } else if("付款状态异常".equals(s)){
            return R.fail("付款状态异常");
        } else {
            return R.success(s);
        }
    }

    @ApiOperation("新增收入-手续费一单一算")
    @ApiOperationSupport(order = 2)
    @PostMapping("/addIncome")
    public R<Boolean> addIncome(StoreIncomeDetailsRecord storeIncome) {
        if (storeIncomeDetailsRecordService.save(storeIncome)) {
            return R.success("新增成功");
        }
        return R.fail("新增失败");
    }

    @ApiOperation("今日收益")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/todayIncome")
    public R<String> todayIncome(Integer storeId) {
        return R.data(storeIncomeDetailsRecordService.todayIncome(storeId));
    }

    @ApiOperation("账期")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "incomeType", value = "收入类型, 0:主页, 1:优惠券, 2:代金券, 3:套餐, 4:联名卡", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "paymentType", value = "账期类型,0:未到,1:已到", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "条数", dataType = "String", paramType = "query")
    })
    @GetMapping("/noYetPayment")
    public R<JSONObject> noYetPayment(Integer storeId, Integer incomeType, Integer paymentType, String startTime, String endTime,
                                      @RequestParam(value = "page", defaultValue = "1") int page,
                                      @RequestParam(value = "size", defaultValue = "10") int size) {
        return R.data(storeIncomeDetailsRecordService.noYetPayment(storeId, incomeType, paymentType,startTime,endTime, page, size));
    }

    @ApiOperation("账户余额-总金额, 可提现金额")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/accountBalance")
    public R<Map<String, Object>> accountBalance(Integer storeId) {
        return R.data(storeIncomeDetailsRecordService.accountBalance(storeId));
    }

    @ApiOperation("团购收益")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "date", value = "日期", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "incomeType", value = "收入类型, 0:主页, 1:优惠券, 2:代金券, 3:套餐, 4:联名卡", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "分页页数", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/groupIncome")
    public R<StoreIncomeDetailsRecordVo> groupIncome(Integer storeId, String date, Integer incomeType,@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        return R.data(storeIncomeDetailsRecordService.groupIncome(storeId, date, incomeType,page,size));
    }


    @ApiOperation("今日订单数")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/todayOrderCount")
    public R<Integer> todayOrderCount(Integer storeId) {
        return R.data(storeIncomeDetailsRecordService.todayOrderCount(storeId));
    }
}
