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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.yitter.idgen.YitIdHelper;
import com.unis.framework.security.core.LoginUser;
import com.unis.framework.security.core.util.SecurityFrameworkUtils;
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.kqxx.vo.*;
import com.unis.module.coding.dal.dataobject.kqxx.KqxxDO;
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.kqxx.KqxxMapper;

import static com.unis.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.unis.module.core.enums.ErrorCodeConstants.KQXX_NOT_EXISTS;
import static com.unis.module.infra.enums.ErrorCodeConstants.ERROR;

/**
 * 库区信息; Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class KqxxServiceImpl implements KqxxService {

    @Resource
    private KqxxMapper kqxxMapper;

    @Override
    public Long createKqxx(KqxxSaveReqVO createReqVO) {
        // 插入
        KqxxDO kqxx = BeanUtils.toBean(createReqVO, KqxxDO.class);
        kqxx.setId(YitIdHelper.nextId());
        kqxx.setSjly(0);
        kqxxMapper.insert(kqxx);
        // 返回
        return kqxx.getId();
    }

    @Override
    public void updateKqxx(KqxxSaveReqVO updateReqVO) {
        // 校验存在
        validateKqxxExists(updateReqVO.getId());
        // 更新
        KqxxDO updateObj = BeanUtils.toBean(updateReqVO, KqxxDO.class);
        kqxxMapper.updateById(updateObj);
    }

    @Override
    public void deleteKqxx(Long id) {
        // 校验存在
        validateKqxxExists(id);
        // 删除
        kqxxMapper.deleteById(id);
    }

    private void validateKqxxExists(Long id) {
        if (kqxxMapper.selectById(id) == null) {
            throw exception(KQXX_NOT_EXISTS);
        }
    }

    @Override
    public KqxxDO getKqxx(Long id) {
        return kqxxMapper.selectById(id);
    }

    @Override
    public PageResult<KqxxDO> getKqxxPage(KqxxPageReqVO pageReqVO) {
        return kqxxMapper.selectPage(pageReqVO);
    }

    @Override
    public KqxxDO getKqxxByKqBm() {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        String kqbm = loginUser.getInfo().get("kqbm");
        if (ObjectUtils.isEmpty(loginUser)) {
            throw exception(ERROR, "不存在用户信息");
        }
        if (ObjectUtils.isEmpty(kqbm)) {
            throw exception(ERROR, "当前用户不存在库区编码");
        }
        List<KqxxDO> KqxxDO = kqxxMapper.selectList(new QueryWrapper<KqxxDO>().eq("KQBM", kqbm));
        if(CollectionUtils.isEmpty(KqxxDO)){
            throw exception(ERROR, "当前库区编码下无库区信息");
        }
        return KqxxDO.get(0);
    }

}