package com.yc.ship.module.otc.service.storescheduling;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.otc.controller.admin.storescheduling.user.StoreSchedulingUserPageReqVO;
import com.yc.ship.module.otc.controller.admin.storescheduling.user.StoreSchedulingUserSaveReqVO;
import com.yc.ship.module.otc.dal.dataobject.storescheduling.StoreSchedulingUserDO;
import com.yc.ship.module.otc.dal.mysql.storescheduling.StoreSchedulingUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.otc.enums.ErrorCodeConstants.STORE_SCHEDULING_USER_NOT_EXISTS;

/**
 * 班次售票员 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class StoreSchedulingUserServiceImpl implements StoreSchedulingUserService {

    @Resource
    private StoreSchedulingUserMapper storeSchedulingUserMapper;

    @Override
    @Transactional
    public Long createStoreSchedulingUser(StoreSchedulingUserSaveReqVO createReqVO) {
        // 插入
        StoreSchedulingUserDO schedulingUserDO = null;
        for (Long userId :createReqVO.getCheckedValues()) {
            schedulingUserDO = new StoreSchedulingUserDO();
            schedulingUserDO.setSchedulingId(createReqVO.getSchedulingId());
            schedulingUserDO.setUserId(userId);
            schedulingUserDO.setId(IdWorker.getId(schedulingUserDO));
            storeSchedulingUserMapper.insert(schedulingUserDO);
        }
//        StoreSchedulingUserDO storeSchedulingUser = BeanUtils.toBean(createReqVO, StoreSchedulingUserDO.class);
//        storeSchedulingUser.setId(IdWorker.getId(storeSchedulingUser));
//        storeSchedulingUserMapper.insert(storeSchedulingUser);
        // 返回
        return createReqVO.getSchedulingId();
    }

    @Override
    public void updateStoreSchedulingUser(StoreSchedulingUserSaveReqVO updateReqVO) {
        // 校验存在
        validateStoreSchedulingUserExists(updateReqVO.getId());
        // 更新
        StoreSchedulingUserDO updateObj = BeanUtils.toBean(updateReqVO, StoreSchedulingUserDO.class);
        storeSchedulingUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteStoreSchedulingUser(Long id) {
        // 校验存在
        validateStoreSchedulingUserExists(id);
        // 删除
        storeSchedulingUserMapper.deleteById(id);
    }

    private void validateStoreSchedulingUserExists(Long id) {
        if (storeSchedulingUserMapper.selectById(id) == null) {
            throw exception(STORE_SCHEDULING_USER_NOT_EXISTS);
        }
    }

    @Override
    public StoreSchedulingUserDO getStoreSchedulingUser(Long id) {
        return storeSchedulingUserMapper.selectById(id);
    }

    @Override
    public PageResult<StoreSchedulingUserDO> getStoreSchedulingUserPage(StoreSchedulingUserPageReqVO pageReqVO) {
        return storeSchedulingUserMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StoreSchedulingUserDO> getSchedulingUserList() {
        return storeSchedulingUserMapper.selectList();
    }

}
