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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starry.admin.common.aspect.ClerkUserLogin;
import com.starry.admin.common.aspect.CustomUserLogin;
import com.starry.admin.common.conf.ThreadLocalRequestDetail;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.order.module.entity.PlayOrderContinueInfoEntity;
import com.starry.admin.modules.order.module.entity.PlayOrderInfoEntity;
import com.starry.admin.modules.order.module.vo.PlayOrderContinueQueryVo;
import com.starry.admin.modules.order.module.vo.PlayOrderContinueReturnVo;
import com.starry.admin.modules.order.module.vo.PlayOrderDetailsReturnVo;
import com.starry.admin.modules.order.module.vo.PlayOrderReviewStateEditVo;
import com.starry.admin.modules.order.service.IPlayOrderContinueInfoService;
import com.starry.admin.modules.order.service.IPlayOrderInfoService;
import com.starry.admin.modules.weichat.entity.order.PlayOrderInfoContinueAdd;
import com.starry.admin.modules.weichat.entity.order.PlayOrderInfoContinueQueryVo;
import com.starry.admin.modules.weichat.entity.order.PlayOrderInfoRandomQueryVo;
import com.starry.admin.modules.weichat.entity.order.PlayRewardOrderQueryVo;
import com.starry.common.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 订单处理
 *
 * @author admin
 * @since 2024/5/25 下午2:45
 **/
@Slf4j
@RestController
@RequestMapping("/wx/order")
public class WxOrderInfoController {

    @Resource
    private IPlayOrderInfoService playOrderInfoService;

    @Resource
    private IPlayOrderContinueInfoService playOrderContinueInfoService;


    /**
     * 续单申请-店员发起
     *
     * @param vo 续单申请提交对象
     */
    @ClerkUserLogin
    @PostMapping("/clerk/continue")
    public R continueToOrdder(@Validated @RequestBody PlayOrderInfoContinueAdd vo) {
        PlayOrderInfoEntity entity = playOrderInfoService.selectOrderInfoById(vo.getOrderId());
        if (!entity.getAcceptBy().equals(ThreadLocalRequestDetail.getClerkUserInfo().getId())) {
            throw new CustomException("非本人订单;无法续单");
        }

        PlayOrderContinueInfoEntity orderContinueInfo = playOrderContinueInfoService.selectPlayOrderId(vo.getOrderId());
        if (orderContinueInfo != null) {
            throw new CustomException("同一场订单只能续单一次");
        }
        PlayOrderContinueInfoEntity orderContinueInfoEntity = new PlayOrderContinueInfoEntity();
        orderContinueInfoEntity.setOrderId(entity.getId());
        orderContinueInfoEntity.setOrderNo(entity.getOrderNo());
        orderContinueInfoEntity.setCustomId(entity.getPurchaserBy());
        orderContinueInfoEntity.setClerkId(entity.getAcceptBy());
        orderContinueInfoEntity.setPlaceType(entity.getPlaceType());
        orderContinueInfoEntity.setOrderMoney(entity.getOrderMoney());
        orderContinueInfoEntity.setFinalAmount(entity.getFinalAmount());
        orderContinueInfoEntity.setContinueMsg(entity.getRemark());
        orderContinueInfoEntity.setReviewedRequired("1");
        orderContinueInfoEntity.setReviewedState("0");
        orderContinueInfoEntity.setImages(vo.getImages());
        orderContinueInfoEntity.setContinueTime(LocalDateTime.now());
        playOrderContinueInfoService.create(orderContinueInfoEntity);
        return R.ok("下单成功");
    }


    /**
     * 店员查询随机单列表
     *
     * @param vo 随机单列表查询对象
     * @return 订单列表
     */
    @ClerkUserLogin
    @PostMapping("/clerk/selectRandomOrderByPage")
    public R selectUnacceptedOrderByPage(@Validated @RequestBody PlayOrderInfoRandomQueryVo vo) {
        return R.ok(playOrderInfoService.selectRandomOrderByPage(vo,ThreadLocalRequestDetail.getClerkUserInfo().getId()));
    }

    /**
     * 店员查询随机单详情
     *
     * @param id 订单ID
     * @return 订单列表
     */
    @ClerkUserLogin
    @GetMapping("/clerk/selectRandomOrderById")
    public R selectUnacceptedOrderByPage(@RequestParam("id") String id) {
        PlayOrderDetailsReturnVo vo = playOrderInfoService.selectById(id);
        if (vo == null) {
            throw new CustomException("订单不存在");
        }
        return R.ok(vo);
    }

    /**
     * 店员查询打赏动态
     *
     * @param vo 打赏动态查询列表
     * @return 打赏动态列表
     */

    @PostMapping("/clerk/selectRewardByPage")
    public R clerkSelectRewardByPage(@Validated @RequestBody PlayRewardOrderQueryVo vo) {
        return R.ok(playOrderInfoService.selectRewardByPage(vo));
    }

    /**
     * 顾客查询最新打赏动态
     *
     * @return 打赏动态列表
     */

    @GetMapping("/custom/selectRewardByPage")
    public R customSelectReward() {
        PlayRewardOrderQueryVo vo = new PlayRewardOrderQueryVo();
        return R.ok(playOrderInfoService.selectRewardByPage(vo));
    }


    /**
     * 顾客-分页查询续单列表
     *
     * @param vo PlayOrderInfoContinueQueryVo
     * @return 续单历史
     */
    @CustomUserLogin
    @PostMapping("/custom/continueListByPage")
    public R continueListByPage(@Validated @RequestBody PlayOrderInfoContinueQueryVo vo) {
        PlayOrderContinueQueryVo queryVo = new PlayOrderContinueQueryVo();
        BeanUtils.copyProperties(vo, queryVo);
        queryVo.setCustomId(ThreadLocalRequestDetail.getCustomUserInfo().getId());
        IPage<PlayOrderContinueReturnVo> page = playOrderContinueInfoService.selectPlayByPage(queryVo);
        return R.ok(page);
    }


    /**
     * 审批续单申申请
     */
    @CustomUserLogin
    @PostMapping("/custom/updateReviewState")
    public R updateReviewState(@Validated @RequestBody PlayOrderReviewStateEditVo vo) {
        PlayOrderContinueInfoEntity entity = playOrderContinueInfoService.selectPlayOrderContinueInfoById(vo.getId());
        if (!"0".equals(entity.getReviewedState())) {
            throw new CustomException("续单已处理");
        }
        playOrderContinueInfoService.updateReviewState(vo);
        return R.ok("成功");
    }

}
