package com.yc.ship.module.trade.service.audituser;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.system.api.user.AdminUserApi;
import com.yc.ship.module.system.api.user.dto.AdminUserRespDTO;
import com.yc.ship.module.trade.controller.admin.audituser.vo.AuditUserPageReqVO;
import com.yc.ship.module.trade.controller.admin.audituser.vo.AuditUserSaveReqVO;
import com.yc.ship.module.trade.controller.admin.bill.vo.BillRespVO;
import com.yc.ship.module.trade.dal.dataobject.audituser.AuditUserDO;
import com.yc.ship.module.trade.dal.mysql.audituser.AuditUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 审核人管理 Service 实现类
 *
 * @author qsl
 */
@Service
@Validated
public class AuditUserServiceImpl implements AuditUserService {

    @Resource
    private AuditUserMapper auditUserMapper;

    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createAuditUser(AuditUserSaveReqVO createReqVO) {
        // 插入
        AuditUserDO auditUser = BeanUtils.toBean(createReqVO, AuditUserDO.class);
        auditUser.setId(IdWorker.getId());
        auditUserMapper.insert(auditUser);
        // 返回
        return auditUser.getId();
    }

    @Override
    public void updateAuditUser(AuditUserSaveReqVO updateReqVO) {
        // 校验存在
        validateAuditUserExists(updateReqVO.getId());
        // 更新
        AuditUserDO updateObj = BeanUtils.toBean(updateReqVO, AuditUserDO.class);
        auditUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteAuditUser(Long id) {
        // 校验存在
        validateAuditUserExists(id);
        // 删除
        auditUserMapper.deleteById(id);
    }

    private void validateAuditUserExists(Long id) {
        if (auditUserMapper.selectById(id) == null) {
            throw exception(500, "不存在");
        }
    }

    @Override
    public AuditUserDO getAuditUser(Long id) {
        return auditUserMapper.selectById(id);
    }

    @Override
    public PageResult<AuditUserDO> getAuditUserPage(AuditUserPageReqVO pageReqVO) {
        IPage<BillRespVO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        IPage<AuditUserDO> pageResult = auditUserMapper.selectPage(page,pageReqVO);
        return new PageResult<>(pageResult.getRecords(), pageResult.getTotal());
    }

}

