package com.yc.ship.module.trade.controller.admin.order;

import com.yc.ship.framework.common.enums.UserTypeEnum;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.collection.MapUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.member.api.user.MemberUserApi;
import com.yc.ship.module.member.api.user.dto.MemberUserRespDTO;
import com.yc.ship.module.system.api.user.AdminUserApi;
import com.yc.ship.module.system.api.user.dto.AdminUserRespDTO;
import com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderLogRespVO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderLogDO;
import com.yc.ship.module.trade.framework.annotation.PlatTenantEnv;
import com.yc.ship.module.trade.service.order.AdminTradeOrderService;
import com.yc.ship.module.trade.service.order.TradeOrderLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.API;

/**
 * 后台管理：订单通用接口
 */
@Validated
@Slf4j
@RestController
@RequestMapping("trade/order")
@Tag(name = "后台管理：订单通用接口")
public class AdminTradeOrderController {

    @Resource
    private TradeOrderLogService tradeOrderLogService;

    @Resource
    private AdminUserApi adminUserApi;

    @Resource
    private MemberUserApi memberUserApi;

    @Resource
    private AdminTradeOrderService adminTradeOrderService;

    @GetMapping("getOrderLogListByOrderId")
    @Operation(summary = "通过订单ID获得订单日志列表")
    public CommonResult<List<TradeOrderLogRespVO>> getOrderLogListByOrderId(@RequestParam("orderNo")  String orderNo) {
        List<TradeOrderLogDO> list = tradeOrderLogService.getOrderLogListByOrderId(orderNo);
        List<TradeOrderLogRespVO> tradeOrderLogRespVoList = BeanUtils.toBean(list, TradeOrderLogRespVO.class);
        List<Long> userIds = CollectionUtils.convertList(tradeOrderLogRespVoList, TradeOrderLogRespVO::getUserId, (item) -> item.getUserType().equals(UserTypeEnum.ADMIN.getValue()));
        List<AdminUserRespDTO> userList = adminUserApi.getUserList(userIds);
        Map<Long, AdminUserRespDTO> userMap = CollectionUtils.convertMap(userList, AdminUserRespDTO::getId);

        List<Long> memberIds = CollectionUtils.convertList(tradeOrderLogRespVoList, TradeOrderLogRespVO::getUserId, (item) -> item.getUserType().equals(UserTypeEnum.MEMBER.getValue()));
        List<MemberUserRespDTO> memberList = memberUserApi.getUserList(memberIds);
        Map<Long, MemberUserRespDTO> memberMap = CollectionUtils.convertMap(memberList, MemberUserRespDTO::getId);

        tradeOrderLogRespVoList.forEach(item -> {
            if(item.getUserType().equals(UserTypeEnum.ADMIN.getValue())) {
                MapUtils.findAndThen(userMap, item.getUserId(), user -> item.setUserName(user.getNickname()));
            }else if(item.getUserType().equals(UserTypeEnum.MEMBER.getValue())) {
                MapUtils.findAndThen(memberMap, item.getUserId(), member -> item.setUserName(member.getNickname()));
            }
        });
        return success(tradeOrderLogRespVoList);
    }
    @PostMapping("/order-apply-invoice")
    @Operation(summary = "订单开票")
    @OperateLog(type = API)
    @PlatTenantEnv
    public CommonResult<?> orderApplyInvoice(@RequestBody String jsonstr){
        return  adminTradeOrderService.orderApplyInvoice(jsonstr);
    }

    @PostMapping("/plat-apply-invoice")
    @Operation(summary = "平台订单开票")
    @OperateLog(type = API)
    @PlatTenantEnv
    public CommonResult<?> platApplyInvoice(@RequestBody String jsonstr){
        return  adminTradeOrderService.platApplyInvoice(jsonstr);
    }
}
