package com.zt.plat.module.contractorder.service.salesorder;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.contractorder.controller.admin.salesorder.vo.SalesOrderDetailPageReqVO;
import com.zt.plat.module.contractorder.controller.admin.salesorder.vo.SalesOrderDetailRespVO;
import com.zt.plat.module.contractorder.controller.admin.salesorder.vo.SalesOrderDetailSaveReqVO;
import com.zt.plat.module.contractorder.dal.dataobject.salesorder.SalesOrderDetailDO;
import com.zt.plat.module.contractorder.dal.mysql.salesorder.SalesOrderDetailMapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.util.object.BeanUtils;


import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;

import static com.zt.plat.module.contractorder.enums.salesorder.ErrorCodeConstants.SALES_ORDER_DETAIL_NOT_EXISTS;

/**
 * 销售订单明细 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class SalesOrderDetailServiceImpl implements SalesOrderDetailService {

    @Resource
    private SalesOrderDetailMapper salesOrderDetailMapper;

    @Override
    @Transactional
    public SalesOrderDetailRespVO createSalesOrderDetail(SalesOrderDetailSaveReqVO createReqVO) {
        // 插入
        SalesOrderDetailDO salesOrderDetail = BeanUtils.toBean(createReqVO, SalesOrderDetailDO.class);
        salesOrderDetail.setOrderId(Long.valueOf(createReqVO.getOrderId()));
        salesOrderDetailMapper.insert(salesOrderDetail);
        // 返回
        return BeanUtils.toBean(salesOrderDetail, SalesOrderDetailRespVO.class);
    }

    @Override
    @Transactional
    public List<SalesOrderDetailRespVO> createSalesOrderDetail(List<SalesOrderDetailSaveReqVO> createReqVOS) {
        List<SalesOrderDetailDO> salesOrderDetailDOS = BeanUtils.toBean(createReqVOS, SalesOrderDetailDO.class);
        salesOrderDetailDOS.forEach(salesOrderDetail -> {
            createReqVOS.forEach(createReqVO -> salesOrderDetail.setOrderId(Long.valueOf(createReqVO.getOrderId())));
        });
        salesOrderDetailMapper.insertBatch(salesOrderDetailDOS);
        return BeanUtils.toBean(salesOrderDetailDOS, SalesOrderDetailRespVO.class);
    }

    @Override
    public void updateSalesOrderDetail(SalesOrderDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateSalesOrderDetailExists(updateReqVO.getId());
        // 更新
        SalesOrderDetailDO updateObj = BeanUtils.toBean(updateReqVO, SalesOrderDetailDO.class);
        salesOrderDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteSalesOrderDetail(Long id) {
        // 校验存在
        validateSalesOrderDetailExists(id);
        // 删除
        salesOrderDetailMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSalesOrderDetailListByIds(List<Long> ids) {
        // 校验存在
        validateSalesOrderDetailExists(ids);
        // 删除
        salesOrderDetailMapper.deleteByIds(ids);
    }

    private void validateSalesOrderDetailExists(List<Long> ids) {
        List<SalesOrderDetailDO> list = salesOrderDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(SALES_ORDER_DETAIL_NOT_EXISTS);
        }
    }

    private void validateSalesOrderDetailExists(Long id) {
        if (salesOrderDetailMapper.selectById(id) == null) {
            throw exception(SALES_ORDER_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public SalesOrderDetailDO getSalesOrderDetail(Long id) {
        return salesOrderDetailMapper.selectById(id);
    }

    @Override
    public PageResult<SalesOrderDetailDO> getSalesOrderDetailPage(SalesOrderDetailPageReqVO pageReqVO) {
        return salesOrderDetailMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSalesOrderDetailByOrderIds(List<Long> orderIds) {
        salesOrderDetailMapper.deleteBatch(SalesOrderDetailDO::getOrderId, orderIds);
    }

    @Override
    public List<SalesOrderDetailRespVO> getSalesOrderDetailByOrderId(Long orderId) {
        return BeanUtils.toBean( salesOrderDetailMapper.selectList(SalesOrderDetailDO::getOrderId, orderId), SalesOrderDetailRespVO.class);
    }


}
