package com.unis.module.coding.service.crkxx;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yitter.idgen.YitIdHelper;
import com.unis.framework.common.util.collection.CollectionUtils;
import com.unis.framework.common.util.kqbm.KQBMUtils;
import com.unis.framework.common.util.object.ObjectUtils;
import com.unis.module.coding.dal.dataobject.hwk.HwkDO;
import com.unis.module.coding.dal.mysql.hwk.HwkMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import com.unis.module.coding.controller.admin.crkxx.vo.*;
import com.unis.module.coding.dal.dataobject.crkxx.CrkxxDO;
import com.unis.framework.common.pojo.PageResult;
import com.unis.framework.common.util.object.BeanUtils;

import com.unis.module.coding.dal.mysql.crkxx.CrkxxMapper;

import java.util.List;

import static com.unis.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.unis.module.core.enums.ErrorCodeConstants.CRKXX_NOT_EXISTS;

/**
 * 出入库信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class CrkxxServiceImpl implements CrkxxService {

    @Resource
    private CrkxxMapper crkxxMapper;
    @Resource
    private HwkMapper hwkMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createCrkxx(CrkxxSaveReqVO createReqVO) {
        CrkxxDO crkxx = BeanUtils.toBean(createReqVO, CrkxxDO.class);
        //列表转成字符串用逗号隔开
        //插入和更新
        if (ObjectUtil.isEmpty(createReqVO.getId())) {
            // 插入前查询一下是否有hwkId
            List<HwkDO> hwks = hwkMapper.selectList(new QueryWrapper<HwkDO>().eq("CF_ID", createReqVO.getCfId()));
            if (CollectionUtil.isNotEmpty(hwks)) {
                //将已经存在的货位卡id放进去
                crkxx.setHwkId(hwks.get(0).getId());
            }
            crkxx.setId(YitIdHelper.nextId());
            crkxx.setDeleted(false);
            crkxxMapper.insert(crkxx);
        } else if (ObjectUtil.isEmpty(createReqVO.getId())) {
            //更改
            crkxxMapper.updateById(crkxx);
        }
        // 返回
        return crkxx.getId();
    }

    @Override
    public void updateCrkxx(CrkxxSaveReqVO updateReqVO) {
        // 校验存在
        validateCrkxxExists(updateReqVO.getId());
        // 更新
        CrkxxDO updateObj = BeanUtils.toBean(updateReqVO, CrkxxDO.class);
        crkxxMapper.updateById(updateObj);
    }

    @Override
    public void deleteCrkxx(Long id) {
        // 校验存在
        validateCrkxxExists(id);
        // 删除
        crkxxMapper.deleteById(id);
    }

    private void validateCrkxxExists(Long id) {
        if (crkxxMapper.selectById(id) == null) {
            throw exception(CRKXX_NOT_EXISTS);
        }
    }

    @Override
    public CrkxxDO getCrkxx(Long id) {
        return crkxxMapper.selectById(id);
    }

    @Override
    public PageResult<CrkxxDO> getCrkxxPage(CrkxxPageReqVO pageReqVO) {
        return crkxxMapper.selectPage(pageReqVO);
    }

}