package com.gree.mall.manager.logic;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.Storage;
import com.gree.mall.manager.plus.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Component
public class StorageLogic {

    @Autowired
    CommonLogic commonLogic;
    @Resource
    StorageService storageService;

    /**
     * 仓储列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public IPage<Storage> page(Integer pageNum, Integer pageSize,HttpServletRequest request) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);

        return storageService.lambdaQuery()
                .page(new Page<>(pageNum, pageSize));
    }

    /**
     * 仓储详情
     *
     * @param id
     * @return
     */
    public Storage detail(String id) {
        return storageService.getById(id);
    }

    public Storage detailByName(String companyWechatId,String name) {
        return storageService
                .lambdaQuery()
                .eq(Storage::getCompanyId, companyWechatId)
                .eq(Storage::getStorageName,name)
                .one();
    }

    /**
     * 新增仓储
     *
     * @param storage
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(HttpServletRequest request,Storage storage) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);

        storage.setCreateTime(new Date());

        storageService.save(storage);
    }

    /**
     * 修改仓储
     *
     * @param storage
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(Storage storage) {
        storage.setCreateTime(null);
        storageService.updateById(storage);
    }

    /**
     * 删除仓储
     *
     * @param storageId
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(String storageId) {
        storageService.removeById(storageId);
    }
}
