package com.shkj.wms.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shkj.common.annotation.Log;
import com.shkj.common.annotation.RepeatSubmit;
import com.shkj.common.core.controller.BaseController;
import com.shkj.common.core.domain.Result;
import com.shkj.common.enums.BusinessType;
import com.shkj.common.enums.IsDelEnum;
import com.shkj.common.utils.SecurityUtils;
import com.shkj.wms.bo.BaseStorageQueryListBo;
import com.shkj.wms.common.SwitchStatusEntity;
import com.shkj.wms.domain.BaseStorage;
import com.shkj.wms.service.IBaseStorageService;
import com.shkj.wms.bo.BaseStorageSaveOrUpdateBo;
import com.shkj.wms.config.ValidList;
import com.shkj.wms.vo.BaseStorageVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 库别资料Controller
 *
 * @author lch
 * @menu 库别资料管理
 * @date 2022-04-23
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wms/storage")
public class BaseStorageController extends BaseController {

    private final IBaseStorageService iBaseStorageService;

    /**
     * 查询库别资料列表
     */
    @GetMapping("/list")
    public Result<List<BaseStorage>> list() {
        return Result.ok().data(iBaseStorageService.list(new LambdaQueryWrapper<BaseStorage>()
                .eq(BaseStorage::getIsDel, IsDelEnum.NORMAL.getValue())
                .eq(BaseStorage::getBranchId, SecurityUtils.getCurrentBranchId())
                .orderByDesc(BaseStorage::getCreateTime)));
    }

    /**
     * 查询库别信息列表(做权限区分)
     */
    @GetMapping("/queryBaseStorageList")
    public Result<List<BaseStorageVo>> queryBaseStorageList(BaseStorageQueryListBo bo) {
        return Result.ok().data(iBaseStorageService.queryBaseStorageList(bo));
    }

    /**
     * 保存或者修改库别信息
     */
    @PostMapping("/saveOrUpdateBaseStorage")
    @RepeatSubmit
    public Result<Boolean> saveOrUpdateBaseStorage(@Valid @RequestBody ValidList<BaseStorageSaveOrUpdateBo> list){
        return iBaseStorageService.saveOrUpdateBaseStorage(list);
    }

    /**
     * 删除指定的库别信息
     * @param ids
     * @return
     */
    @PostMapping("/removeBaseStorage")
    public Result<Boolean> removeBaseStorage(@Valid @RequestBody ValidList<Long> ids){
        return iBaseStorageService.deleteWithValidById(ids,true);
    }


    /**
     * 切换状态
     */
    @Log(title = "切换状态" , businessType = BusinessType.UPDATE)
    @PostMapping("/switchStatus")
    public Result<Boolean> switchStatus(@Valid @RequestBody SwitchStatusEntity switchStatusEntity) {
        return iBaseStorageService.switchStatus(switchStatusEntity.getId(), switchStatusEntity.getIsuse());
    }
}
