package com.qmth.sop.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.sop.business.entity.SysSupplier;
import com.qmth.sop.business.entity.SysUser;
import com.qmth.sop.business.mapper.SysSupplierMapper;
import com.qmth.sop.business.service.SysSupplierService;
import com.qmth.sop.common.enums.SupplierTypeEnum;
import com.qmth.sop.common.enums.UserRoleEnum;
import com.qmth.sop.common.util.ServletUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 供应商表 服务实现.
 *
 * @author: shudonghui
 * @date: 2023-08-02 14:18:39
 * @version: 1.0
 * @email: shudonghui@qmth.com.cn
 * @Company: www.qmth.com.cn
 */
@Service
public class SysSupplierServiceImpl extends ServiceImpl<SysSupplierMapper, SysSupplier> implements SysSupplierService {


    /**
     * 查询列表
     *
     * @param iPage
     * @return
     */
    @Override
    public IPage<SysSupplier> query(IPage<Map> iPage) {
        return this.baseMapper.query(iPage);
    }

    /**
     * 新增供应商表
     *
     * @param sysSupplier
     */
    @Override
    @Transactional
    public void add(SysSupplier sysSupplier) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        sysSupplier.setCreateId(sysUser.getId());
        sysSupplier.setCreateTime(System.currentTimeMillis());
        this.saveOrUpdate(sysSupplier);
    }

    /**
     * 修改供应商表
     *
     * @param sysSupplier
     */
    @Override
    @Transactional
    public void update(SysSupplier sysSupplier) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        sysSupplier.setUpdateId(sysUser.getId());
        sysSupplier.setUpdateTime(System.currentTimeMillis());
        this.saveOrUpdate(sysSupplier);
    }

    /**
     * 删除供应商表
     *
     * @param id
     */
    @Override
    @Transactional
    public void delete(Long id) {
        this.removeById(id);
    }

    @Override
    @Transactional
    public void enable(long id) {
        SysSupplier sysSupplier = this.getById(id);
        sysSupplier.setEnable(!sysSupplier.getEnable());
        this.update(sysSupplier);
    }

    /**
     * 根据userRole查找供应商
     *
     * @param userRoleEnum
     * @return
     */
    @Override
    public List<SysSupplier> findByUserRoleEnum(UserRoleEnum userRoleEnum) {
        QueryWrapper<SysSupplier> sysSupplierQueryWrapper = new QueryWrapper<>();
        switch (userRoleEnum) {
            case THIRD_PMO:
                sysSupplierQueryWrapper.lambda().eq(SysSupplier::getType, SupplierTypeEnum.HUMAN);
                break;
            case DEVICE_DELIVERY:
                sysSupplierQueryWrapper.lambda().eq(SysSupplier::getType, SupplierTypeEnum.DEVICE);
                break;
            default:
                break;
        }
        return this.list(sysSupplierQueryWrapper);
    }
}