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 org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 店员工资结算信息Controller
 *
 * @author admin
 * @since 2024-05-31
 */
@RestController
@RequestMapping("/clerk/wages")
public class PlayClerkWagesInfoController {
    @Resource
    private IPlayClerkWagesInfoService playClerkWagesInfoService;

    @Resource
    private IPlayClerkWagesDetailsInfoService playClerkWagesDetailsInfoService;

    @Resource
    private IPlayClerkUserInfoService playClerkUserInfoService;

    /**
     * 查询店员工资结算信息列表
     */
    @PostMapping("/listByPage")
    public R list(@Validated @RequestBody PlayClerkWagesInfoQueryVo vo) {
        return R.ok(playClerkWagesInfoService.selectHistoricalByPage(vo));
    }

    /**
     * 查询店员工未结算工资
     */
    @PostMapping("/listUnsettledWagesByPage")
    public R list(@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);
    }


    @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);
    }


}
