package com.zt.plat.module.base.service.plandate;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.base.controller.admin.plandate.vo.PlanDateRespVO;
import com.zt.plat.module.base.controller.admin.plandate.vo.PlanOrderRspVO;
import com.zt.plat.module.base.controller.admin.plandate.vo.PlanOrderSaveReqVO;
import com.zt.plat.module.base.dal.dao.plandate.PlanOrderMapper;
import com.zt.plat.module.base.dal.dataobject.plandate.PlanOrderDO;
import com.zt.plat.module.contractorder.api.OrderApi;
import com.zt.plat.module.contractorder.api.dto.order.OrderAndPlanDataReqDTO;
import com.zt.plat.module.contractorder.api.dto.order.OrderDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.base.enums.ErrorCodeConstants.*;

@Service
@Validated
public class PlanOrderServiceImpl implements PlanOrderService {
    @Resource
    private PlanOrderMapper planOrderMapper;

    @Resource
    private OrderApi orderApi;

    @Override
    public PlanOrderRspVO bindOrder(PlanOrderSaveReqVO reqVO) {
        PlanOrderDO bean = BeanUtils.toBean(reqVO, PlanOrderDO.class);
        //校验当前计划下是否已存在该类型的订单
        validateExists(reqVO);
        planOrderMapper.insert(bean);
        return BeanUtils.toBean(bean, PlanOrderRspVO.class);
    }


    @Override
    public void unbindOrder(String planId, String orderNumber) {
        //检验存在
        validateExists(planId, orderNumber);
        planOrderMapper.delete(new LambdaQueryWrapper<PlanOrderDO>().eq(PlanOrderDO::getPlanId, planId).eq(PlanOrderDO::getOrderNumber, orderNumber));
    }

    @Override
    public PageResult<OrderDTO>  getBoundOrder(String id) {
        OrderAndPlanDataReqDTO reqVO = new OrderAndPlanDataReqDTO();
        reqVO.setPlanId(id);
        reqVO.setPageNo(0);
        reqVO.setPageNo(10000);
        CommonResult<PageResult<OrderDTO>> pageResultCommonResult = orderApi.queryOrderPlanData(reqVO);
        return pageResultCommonResult.getData();
    }

    @Override
    public PageResult<OrderDTO> queryOrderAndPlanData(OrderAndPlanDataReqDTO reqVO) {
        CommonResult<PageResult<OrderDTO>> pageResultCommonResult = orderApi.queryOrderPlanData(reqVO);
        return pageResultCommonResult.getData();
    }

    private void validateExists(PlanOrderSaveReqVO reqVO) {
        if (planOrderMapper.exists(new LambdaQueryWrapper<PlanOrderDO>()
                .eq(PlanOrderDO::getPlanId, reqVO.getPlanId())
                .eq(PlanOrderDO::getOrderType, reqVO.getOrderType())
                .eq(PlanOrderDO::getOrderNumber, reqVO.getOrderNumber()))) {
            throw exception(PLAN_ORD_EXISTS);
        }
    }

    private void validateExists(String planId, String orderNumber) {
        if (planOrderMapper.selectOne(new LambdaQueryWrapper<PlanOrderDO>().eq(PlanOrderDO::getPlanId, planId).eq(PlanOrderDO::getOrderNumber, orderNumber),false) == null) {
            throw exception(PLAN_ORD_NOT_EXISTS);
        }
    }
}

