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

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.module.coding.dal.dataobject.hwk.HwkDO;
import com.unis.module.coding.dal.dataobject.lqjcxx.LqjcxxDO;
import com.unis.module.coding.dal.dataobject.zjxx.ZjxxDO;
import com.unis.module.coding.dal.mysql.hwk.HwkMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

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

import java.util.*;

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

import com.unis.module.coding.dal.mysql.syxx.SyxxMapper;

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

/**
 * 损益信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class SyxxServiceImpl implements SyxxService {

    @Resource
    private SyxxMapper syxxMapper;
    @Resource
    private HwkMapper hwkMapper;

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

    @Override
    public void updateSyxx(SyxxSaveReqVO updateReqVO) {
        // 校验存在
        validateSyxxExists(updateReqVO.getId());
        // 更新
        SyxxDO updateObj = BeanUtils.toBean(updateReqVO, SyxxDO.class);
        syxxMapper.updateById(updateObj);
    }

    @Override
    public void deleteSyxx(Long id) {
        // 校验存在
        validateSyxxExists(id);
        // 删除
        syxxMapper.deleteById(id);
    }

    private void validateSyxxExists(Long id) {
        if (syxxMapper.selectById(id) == null) {
            throw exception(SYXX_NOT_EXISTS);
        }
    }

    @Override
    public SyxxDO getSyxx(Long id) {
        return syxxMapper.selectById(id);
    }

    @Override
    public PageResult<SyxxDO> getSyxxPage(SyxxPageReqVO pageReqVO) {
        return syxxMapper.selectPage(pageReqVO);
    }

    @Override
    public SyxxDO getSyxxByCfId(Long cfId) {
        List<SyxxDO> syxxDOS = syxxMapper.selectList(new QueryWrapper<SyxxDO>().eq("CF_ID", cfId));
        SyxxDO syxxDO = null;
        if (!CollectionUtil.isEmpty(syxxDOS)) {
            syxxDO = syxxDOS.get(0);
        }
        return syxxDO;
    }

}