package com.ruoyi.web.controller.storageLocation;

import com.ruoyi.clMaterial.domain.CYlMaterial;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.storageLocation.domain.StorageLocation;
import com.ruoyi.storageLocation.service.StorageLocationService;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 库位管理Controller
 */
@RestController
@RequestMapping("/storageLocation/storageLocation")
@DataSource(DataSourceType.STORAGE)
public class storageLocationController extends BaseController
{

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private StorageLocationService storageLocationService;

    @Autowired
    private ISysConfigService configService;

    /**
     * 获取库位信息
     */
    @GetMapping("/list")
    public AjaxResult list(StorageLocation storageLocation)
    {
        AjaxResult ajaxResult = new AjaxResult();
        List<StorageLocation> listA = storageLocationService.selectStorageLocationAList(storageLocation);
        List<StorageLocation> listB = storageLocationService.selectStorageLocationBList(storageLocation);
        List<StorageLocation> listC = storageLocationService.selectStorageLocationCList(storageLocation);

        ajaxResult.put("materialDetailA",listA);
        ajaxResult.put("materialDetailB",listB);
        ajaxResult.put("materialDetailC",listC);
        return ajaxResult;
    }

    /**
     * 录入库位信息
     */
    @Log(title = "录入库位信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StorageLocation storageLocation)
    {
        String user_name = getLoginUser().getUsername();//当前登陆者
        String materialBh = storageLocation.getMaterialBh();

        String kw_user_name = redisCache.getCacheObject(getCacheKey(materialBh));
        if(StringUtils.isNull(kw_user_name)){
            return error("已超过有效录入信息时间，请关闭重新录入!");
        }else if(!user_name.equals(kw_user_name)){
            return error("已超过有效录入信息时间，当前库位有人正在操作!");
        }else{
            redisCache.setCacheObject(getCacheKey(materialBh),user_name);
            return toAjax(storageLocationService.addStorageLocation(storageLocation));
        }

    }

    /**
     * 校验库位是否已占用
     */
    @Log(title = "校验库位是否已占用", businessType = BusinessType.OTHER)
    @PostMapping("/checkStorageLocation")
    public AjaxResult checkStorageLocation(@RequestBody StorageLocation storageLocation)
    {
        String storageExpire = configService.selectConfigByKey("storage.expire");

        String user_name = getLoginUser().getUsername();//当前登陆者
        String materialBh = storageLocation.getMaterialBh();

        String kw_user_name = redisCache.getCacheObject(getCacheKey(materialBh));
        if(StringUtils.isNotNull(kw_user_name)&&!user_name.equals(kw_user_name)){
            return error("当前库位有人正在操作,请勿操作!");
        }else{
            redisCache.setCacheObject(getCacheKey(materialBh),user_name,Integer.valueOf(storageExpire), TimeUnit.SECONDS);
            return success();
        }
    }

    /**
     * 弹窗取消按钮 清除库位缓存
     */
    @Log(title = "校验库位是否已占用", businessType = BusinessType.OTHER)
    @PostMapping("/cancelStorageLocation")
    public AjaxResult cancelStorageLocation(@RequestBody StorageLocation storageLocation)
    {
        String storageExpire = configService.selectConfigByKey("storage.expire");

        String user_name = getLoginUser().getUsername();//当前登陆者
        String materialBh = storageLocation.getMaterialBh();

        String kw_user_name = redisCache.getCacheObject(getCacheKey(materialBh));
        if(StringUtils.isNotNull(kw_user_name)&&user_name.equals(kw_user_name)){
            redisCache.deleteObject(getCacheKey(materialBh));
        }
        return success();
    }

    /**
     * 库位缓存键名
     *
     * @param materialBh 库位号
     * @return 缓存键key
     */
    private String getCacheKey(String materialBh)
    {
        return CacheConstants.STORAGE_LOCATION + materialBh;
    }

}
