package cn.iocoder.yudao.module.stockOperate.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.common.RepairErrorCodeConstants;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.company.entity.Company;
import cn.iocoder.yudao.module.company.service.CompanyService;
import cn.iocoder.yudao.module.stockOperate.entity.DlRepairSo;
import cn.iocoder.yudao.module.stockOperate.mapper.DlRepairSoMapper;
import cn.iocoder.yudao.module.stockOperate.service.DlRepairSoService;
import cn.iocoder.yudao.module.stockOperate.service.DlRepairSoiService;
import cn.iocoder.yudao.module.stockOperate.vo.DlRepairSoReqVO;
import cn.iocoder.yudao.module.stockOperate.vo.DlRepairSoRespVO;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 针对表【dl_repair_so(采购单领料单)】的数据库操作Service实现
 *
 * @author 小李
 * @date 9:11 2024/9/13
 **/
@Service
public class DlRepairSoServiceImpl extends ServiceImpl<DlRepairSoMapper, DlRepairSo>
        implements DlRepairSoService {

    @Resource
    private DlRepairSoiService repairSoiService;

    @Resource
    private DeptApi deptApi;

    @Resource
    private CompanyService companyService;

    /**
     * 采购单/领料单 新增
     * @author 小李
     * @date 10:49 2024/9/14
     * @param repairSoRespVO 采购单对象
     **/
    @DSTransactional
    @Override
    public void createRepairSo(DlRepairSoRespVO repairSoRespVO){
        // 取当前登录用户的门店信息
        Long deptId = SecurityFrameworkUtils.getLoginUserDeptId();
        repairSoRespVO.setDeptId(deptId);

        DeptRespDTO dept = deptApi.getDept(deptId);
        if (ObjectUtil.isNotEmpty(dept)){
            repairSoRespVO.setCorpId(dept.getCorpId());
            Company company = companyService.getById(dept.getCorpId());
            repairSoRespVO.setCorpName(company.getCorpName());
        }

        // 新增主表
        baseMapper.insertOrUpdate(repairSoRespVO);
        // 新增子表
        if (CollectionUtil.isEmpty(repairSoRespVO.getGoodsList())){
            throw exception(RepairErrorCodeConstants.GOODS_IS_EMPTY);
        }
        repairSoRespVO.getGoodsList().forEach(item -> item.setSoId(repairSoRespVO.getId()));
        repairSoiService.saveBatch(repairSoRespVO.getGoodsList());
    }

    /**
     * 采购单/领料单新增分页
     *
     * @param repairSoReqVO 查询对象
     * @author 小李
     * @date 18:14 2024/9/14
     **/
    @Override
    public IPage<DlRepairSo> getRepairSoPage(DlRepairSoReqVO repairSoReqVO, Page<DlRepairSo> page){
        return baseMapper.getRepairSoPage(repairSoReqVO, page);
    }

    /**
     * 采购单/领料单 作废
     *
     * @author 小李
     * @date 11:12 2024/9/18
     * @param repairSoReqVO 作废对象
     **/
    @Override
    public void voidRepairSo(DlRepairSoReqVO repairSoReqVO){
        baseMapper.updateById(repairSoReqVO);
    }
}




