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

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.clerk.module.entity.PlayClerkWagesDetailsInfoEntity;
import com.starry.admin.modules.clerk.module.vo.PlayClerkUnsettledWagesInfoQueryVo;
import com.starry.admin.modules.clerk.module.vo.PlayClerkUnsettledWagesInfoReturnVo;
import com.starry.admin.modules.clerk.module.vo.PlayClerkWagesInfoQueryVo;
import com.starry.admin.modules.clerk.service.IPlayClerkUserInfoService;
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.weichat.entity.wages.ClerkWagesDetailsReturnVo;
import com.starry.common.result.R;
import com.starry.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 店员工资结算信息Controller
 *
 * @author admin
 * @since 2024-05-31
 */
@Api(tags = "店员工资结算", description = "店员工资结算信息管理相关接口，包括查询历史工资、未结算工资和工资明细等")
@RestController
@RequestMapping("/clerk/wages")
public class PlayClerkWagesInfoController {
    @Resource
    private IPlayClerkWagesInfoService playClerkWagesInfoService;

    @Resource
    private IPlayClerkWagesDetailsInfoService playClerkWagesDetailsInfoService;

    @Resource
    private IPlayClerkUserInfoService playClerkUserInfoService;

    /**
     * 查询店员工资结算信息列表
     */
    @ApiOperation(value = "查询历史工资列表", notes = "分页查询店员历史工资结算信息列表")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = Object.class, responseContainer = "Page")})
    @PostMapping("/listByPage")
    public R list(@ApiParam(value = "查询条件", required = true) @Validated @RequestBody PlayClerkWagesInfoQueryVo vo) {
        return R.ok(playClerkWagesInfoService.selectHistoricalByPage(vo));
    }

    /**
     * 查询店员工未结算工资
     */
    @ApiOperation(value = "查询未结算工资", notes = "分页查询店员未结算工资信息列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = PlayClerkUnsettledWagesInfoReturnVo.class, responseContainer = "Page")})
    @PostMapping("/listUnsettledWagesByPage")
    public R list(
            @ApiParam(value = "查询条件", required = true) @Validated @RequestBody PlayClerkUnsettledWagesInfoQueryVo vo) {
        IPage<PlayClerkUnsettledWagesInfoReturnVo> page = playClerkUserInfoService.listUnsettledWagesByPage(vo);
        for (PlayClerkUnsettledWagesInfoReturnVo record : page.getRecords()) {
            Integer orderState1Number = 0;
            BigDecimal orderState1Money = BigDecimal.ZERO;
            Integer orderState2Number = 0;
            BigDecimal orderState2Money = BigDecimal.ZERO;
            BigDecimal orderState2Revenue = BigDecimal.ZERO;
            Integer orderState3Number = 0;
            BigDecimal orderState3Money = BigDecimal.ZERO;
            BigDecimal orderState3Revenue = BigDecimal.ZERO;
            for (PlayOrderInfoEntity orderInfoEntity : record.getOrderInfoEntities()) {
                if ("1".equals(orderInfoEntity.getOrderStatus())) {
                    orderState1Number++;
                    orderState1Money = orderState1Money.add(orderInfoEntity.getFinalAmount());
                } else if ("2".equals(orderInfoEntity.getOrderStatus())) {
                    orderState2Number++;
                    orderState2Money = orderState2Money.add(orderInfoEntity.getFinalAmount());
                    orderState2Revenue = orderState2Revenue.add(orderInfoEntity.getEstimatedRevenue());
                } else if ("3".equals(orderInfoEntity.getOrderStatus())) {
                    orderState3Number++;
                    orderState3Money = orderState3Money.add(orderInfoEntity.getFinalAmount());
                    orderState3Revenue = orderState3Revenue.add(orderInfoEntity.getEstimatedRevenue());
                }
            }
            record.setOrderState1Number(orderState1Number);
            record.setOrderState1Money(orderState1Money);
            record.setOrderState2Number(orderState2Number);
            record.setOrderState2Money(orderState2Money);
            record.setOrderState2Revenue(orderState2Revenue);
            record.setOrderState3Number(orderState3Number);
            record.setOrderState3Money(orderState3Money);
            record.setOrderState3Revenue(orderState3Revenue);
        }
        return R.ok(page);
    }

    @ApiOperation(value = "查询工资明细", notes = "根据工资结算ID查询工资明细信息")
    @ApiImplicitParam(name = "id", value = "工资结算ID", required = true, paramType = "query", dataType = "String", example = "1")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = ClerkWagesDetailsReturnVo.class, responseContainer = "List"),
            @ApiResponse(code = 500, message = "ID不能为空")})
    @GetMapping("queryWagesDetailsById")
    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);
    }

}
