package com.starry.admin.modules.weichat.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.starry.admin.common.aspect.ClerkUserLogin;
import com.starry.admin.common.conf.ThreadLocalRequestDetail;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.clerk.module.entity.PlayClerkWagesDetailsInfoEntity;
import com.starry.admin.modules.clerk.module.entity.PlayClerkWagesInfoEntity;
import com.starry.admin.modules.clerk.service.IPlayClerkWagesDetailsInfoService;
import com.starry.admin.modules.clerk.service.IPlayClerkWagesInfoService;
import com.starry.admin.modules.order.module.entity.PlayOrderInfoEntity;
import com.starry.admin.modules.order.service.IPlayOrderInfoService;
import com.starry.admin.modules.weichat.entity.wages.*;
import com.starry.common.result.R;
import com.starry.common.utils.ConvertUtil;
import com.starry.common.utils.IdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 店员工资接口
 *
 * @author admin
 * @since 2024/5/31 16:18
 **/
@Api(tags = "微信店员工资接口", description = "微信端店员工资相关接口")
@Slf4j
@RestController
@RequestMapping("/wx/wages")
public class WxClerkWagesController {
    @Resource
    private IPlayClerkWagesInfoService playClerkWagesInfoService;

    @Resource
    private IPlayClerkWagesDetailsInfoService playClerkWagesDetailsInfoService;

    @Resource
    private IPlayOrderInfoService playOrderInfoService;

    /**
     * 店员查询本人未结算工资 订单完成之后，24小时之后进行订单结算，并且一旦订单结算了，就无法进行退款。想要退款，只能在24小时之内进行。
     *
     * @author admin
     * @since 2024/5/31 16:20
     **/
    @ApiOperation(value = "查询未结算工资", notes = "店员查询本人未结算工资")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = ClerkUnsettledWagesReturnVo.class)})
    @ClerkUserLogin
    @GetMapping("clerk/queryUnsettledWages")
    public R clerkQueryUnsettledWages() {
        List<PlayOrderInfoEntity> list = playOrderInfoService
                .queryBySettlementOrder(ThreadLocalRequestDetail.getClerkUserInfo().getId(), "0");
        BigDecimal orderMoney = BigDecimal.ZERO;
        BigDecimal estimatedRevenue = BigDecimal.ZERO;
        for (PlayOrderInfoEntity entity : list) {
            orderMoney = orderMoney.add(entity.getFinalAmount());
            estimatedRevenue = estimatedRevenue.add(entity.getEstimatedRevenue());
        }
        ClerkUnsettledWagesReturnVo vo = new ClerkUnsettledWagesReturnVo(orderMoney, list.size(), estimatedRevenue);
        return R.ok(vo);
    }

    /**
     * 店员查询本人当期工资
     *
     * @author admin
     * @since 2024/5/31 16:20
     **/
    @ApiOperation(value = "查询当期工资", notes = "店员查询本人当期工资")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = ClerkCurrentPeriodWagesReturnVo.class)})
    @ClerkUserLogin
    @GetMapping("clerk/queryCurrentPeriodWages")
    public R clerkQueryCurrentPeriodWages() {
        PlayClerkWagesInfoEntity entity = playClerkWagesInfoService
                .selectCurrentPeriodWagesByClerkId(ThreadLocalRequestDetail.getClerkUserInfo().getId());
        if (entity == null) {
            entity = new PlayClerkWagesInfoEntity();
            entity.setClerkId(ThreadLocalRequestDetail.getClerkUserInfo().getId());
            entity.setId(IdUtils.getUuid());
            entity.setStartCountDate(LocalDate.now());
            entity.setEndCountDate(LocalDate.now());
            entity.setFinalAmount(BigDecimal.ZERO);
            entity.setOrderNumber(0);
            entity.setOrdersExpiredNumber(0);
            entity.setOrderContinueNumber(0);
            entity.setOrderContinueProportion(0);
            entity.setOrderContinueMoney(BigDecimal.ZERO);
        }
        ClerkCurrentPeriodWagesReturnVo returnVo = ConvertUtil.entityToVo(entity,
                ClerkCurrentPeriodWagesReturnVo.class);
        returnVo.setTotalMoney(entity.getFinalAmount());
        returnVo.setOrderWages(ConvertUtil.entityToVo(entity, OrderWagesReturnVo.class));
        return R.ok(returnVo);
    }

    /**
     * 店员查询本人历史工资
     *
     * @author admin
     * @since 2024/5/31 16:20
     **/
    @ApiOperation(value = "查询历史工资", notes = "店员查询本人历史工资")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = ClerkHistoricalWagesReturnVo.class, responseContainer = "Page")})
    @ClerkUserLogin
    @PostMapping("clerk/queryHistoricalWages")
    public R clerkQueryHistoricalWages() {
        List<PlayClerkWagesInfoEntity> list = playClerkWagesInfoService
                .selectHistoricalWagesByClerkId(ThreadLocalRequestDetail.getClerkUserInfo().getId());
        List<ClerkHistoricalWagesReturnVo> returnVoList = new ArrayList<>(list.size());
        for (PlayClerkWagesInfoEntity entity : list) {
            returnVoList.add(ConvertUtil.entityToVo(entity, ClerkHistoricalWagesReturnVo.class));
        }
        IPage<ClerkHistoricalWagesReturnVo> page = new Page<>();
        page.setTotal(5);
        page.setRecords(returnVoList);
        page.setSize(10);
        page.setPages(1);
        return R.ok(page);

    }

    /**
     * 店员查询本人工资详情
     *
     * @author admin
     * @since 2024/5/31 16:20
     **/
    @ApiOperation(value = "查询工资详情", notes = "店员查询本人工资详情")
    @ApiImplicitParam(name = "id", value = "工资ID", required = true, dataType = "String", paramType = "query")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = ClerkWagesDetailsReturnVo.class, responseContainer = "List"),
            @ApiResponse(code = 500, message = "ID不能为空")})
    @ClerkUserLogin
    @GetMapping("clerk/queryWagesDetails")
    public R clerkQueryWagesDetails(@RequestParam("id") String id) {
        if (StrUtil.isBlankIfStr(id)) {
            throw new CustomException("ID不能为空");
        }
        List<PlayClerkWagesDetailsInfoEntity> list = playClerkWagesDetailsInfoService.selectByWagesId(id);
        List<ClerkWagesDetailsReturnVo> returnVos = new ArrayList<>(list.size());
        for (PlayClerkWagesDetailsInfoEntity entity : list) {
            returnVos.add(ConvertUtil.entityToVo(entity, ClerkWagesDetailsReturnVo.class));
        }
        return R.ok(returnVos);
    }
}
