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

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.pojo.PageResult;
import com.unis.framework.common.util.object.BeanUtils;
import com.unis.module.coding.controller.admin.lqjcxx.vo.LqjcxxPageReqVO;
import com.unis.module.coding.controller.admin.lqjcxx.vo.LqjcxxSaveReqVO;
import com.unis.module.coding.dal.dataobject.hwk.HwkDO;
import com.unis.module.coding.dal.dataobject.lqjcxx.LqjcxxDO;
import com.unis.module.coding.dal.mysql.hwk.HwkMapper;
import com.unis.module.coding.dal.mysql.lqjcxx.LqjcxxMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.Collection;
import java.util.List;

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

/**
 * 粮情检测信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class LqjcxxServiceImpl implements LqjcxxService {

    @Resource
    private LqjcxxMapper lqjcxxMapper;
    @Resource
    private HwkMapper hwkMapper;


    @Override
    public Long createLqjcxx(LqjcxxSaveReqVO createReqVO) {
        LqjcxxDO lqjcxx = BeanUtils.toBean(createReqVO, LqjcxxDO.class);
        if (ObjectUtil.isEmpty(createReqVO.getId())) {
            // 插入
            lqjcxx.setId(YitIdHelper.nextId());
            // 插入前查询一下是否有hwkId
            List<HwkDO> hwks = hwkMapper.selectList(new QueryWrapper<HwkDO>().eq("CF_ID", createReqVO.getCfId()));
            if (CollectionUtil.isNotEmpty(hwks)) {
                //将已经存在的货位卡id放进去
                lqjcxx.setHwkId(hwks.get(0).getId());
            }
            //插入
            lqjcxxMapper.insert(lqjcxx);
        } else {
            lqjcxxMapper.updateById(lqjcxx);
        }
        // 返回
        return lqjcxx.getId();
    }

    @Override
    public void updateLqjcxx(LqjcxxSaveReqVO updateReqVO) {
        // 校验存在
        validateLqjcxxExists(updateReqVO.getId());
        // 更新
        LqjcxxDO updateObj = BeanUtils.toBean(updateReqVO, LqjcxxDO.class);
        lqjcxxMapper.updateById(updateObj);
    }

    @Override
    public void deleteLqjcxx(Long id) {
        // 校验存在
        validateLqjcxxExists(id);
        // 删除
        lqjcxxMapper.deleteById(id);
    }

    private void validateLqjcxxExists(Long id) {
        if (lqjcxxMapper.selectById(id) == null) {
            throw exception(LQJCXX_NOT_EXISTS);
        }
    }

    @Override
    public LqjcxxDO getLqjcxx(Long id) {
        return lqjcxxMapper.selectById(id);
    }

    @Override
    public PageResult<LqjcxxDO> getLqjcxxPage(LqjcxxPageReqVO pageReqVO) {
        return lqjcxxMapper.selectPage(pageReqVO);
    }

    @Override
    public LqjcxxDO getLqjcxxByCfId(Long cfId) {
        List<LqjcxxDO> cf_id = lqjcxxMapper.selectList(new QueryWrapper<LqjcxxDO>().eq("CF_ID", cfId));
        LqjcxxDO lqjcxxDO = null;
        if (!CollectionUtils.isEmpty(cf_id)) {
            lqjcxxDO = cf_id.get(0);
        }
        return lqjcxxDO;
    }

}