package com.hys.app.controller.erp.use;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hys.app.framework.context.user.AdminUserContext;
import com.hys.app.framework.database.BaseQueryParam;
import com.hys.app.framework.database.WebPage;
import com.hys.app.model.erp.dos.WarehouseAreaDO;
import com.hys.app.model.erp.dos.WarehouseAreaTypeDO;
import com.hys.app.model.erp.dto.WarehouseAreaQueryParams;
import com.hys.app.model.erp.dto.WarehouseAreaRequest;
import com.hys.app.model.erp.vo.ProductStockVO;
import com.hys.app.model.erp.vo.WarehouseAreaVO;
import com.hys.app.model.support.LogClient;
import com.hys.app.model.support.validator.annotation.Log;
import com.hys.app.service.erp.WarehouseAreaManager;
import com.hys.app.service.erp.WarehouseAreaTypeManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 出库单明细相关API
 *
 * @author 张崧
 * @since 2023-12-07 17:06:32
 */
@RestController
@RequestMapping("/admin/erp/warehouse/area")
@Api(tags = "库位管理相关接口")
@Validated
public class WarehouseAreaController {

    @Autowired
    private WarehouseAreaManager warehouseAreaManager;

    @Autowired
    private WarehouseAreaTypeManager warehouseAreaTypeManager;


    @ApiOperation(value = "获取库位列表")
    @GetMapping("/query")
    public WebPage<WarehouseAreaVO> list(WarehouseAreaQueryParams queryParams) {
        AdminUserContext.hasSuperAdminQuery(queryParams.getCompanyId());
        return warehouseAreaManager.list(queryParams);
    }


    @ApiOperation(value = "添加")
    @PostMapping("/create")
    @Log(client = LogClient.admin, detail = "创建库位信息")
    public void add(@RequestBody @Valid WarehouseAreaDO warehouseAreaDO) {
        AdminUserContext.hasCompany();
        warehouseAreaManager.add(warehouseAreaDO);
    }

    @ApiOperation(value = "更新")
    @PostMapping("/update")
    @Log(client = LogClient.admin, detail = "修改id为[${id}]的库位信息")
    public void update(@RequestBody @Valid WarehouseAreaDO warehouseAreaDO) {
        AdminUserContext.hasCompany();
        warehouseAreaManager.edit(warehouseAreaDO);
    }

    @ApiOperation(value = "库位详情")
    @PostMapping("/info")
    public WarehouseAreaVO info(@RequestBody @Valid WarehouseAreaRequest request) {
        return warehouseAreaManager.getDetail(request.getId());
    }


    @ApiOperation(value = "删除")
    @DeleteMapping("/delete")
    @Log(client = LogClient.admin, detail = "删除id为[${id}]的库位信息")
    public void delete(@RequestBody @Valid WarehouseAreaRequest request) {
        AdminUserContext.hasCompany();
        warehouseAreaManager.delete(request.getId());
    }

    @ApiOperation(value = "添加库位类型")
    @PostMapping("/createAreaType")
    @Log(client = LogClient.admin, detail = "添加名称为[${warehouseAreaTypeDO.name}]的库位信息")
    public void createAreaType(@RequestBody @Valid WarehouseAreaTypeDO warehouseAreaTypeDO) {
        AdminUserContext.hasCompany();
        warehouseAreaTypeDO.setCompanyId(AdminUserContext.getCompanyId());
        warehouseAreaTypeManager.save(warehouseAreaTypeDO);
    }

    @ApiOperation(value = "删除库位类型")
    @DeleteMapping("/removeAreaType/{id}")
    @Log(client = LogClient.admin, detail = "删除id为为[${id}]的库位信息")
    public void removeAreaType(@PathVariable Long id) {
        AdminUserContext.hasCompany();
        warehouseAreaTypeManager.removeById(id);
    }

    @ApiOperation(value = "修改库位类型")
    @PutMapping("/updateAreaType/{id}")
    @Log(client = LogClient.admin, detail = "修改id为[${id}]的库位信息")
    public void updateAreaType(@RequestBody @Valid WarehouseAreaTypeDO warehouseAreaTypeDO, @PathVariable(value = "id") Long id) {
        AdminUserContext.hasCompany();
        warehouseAreaTypeDO.setId(id);
        warehouseAreaTypeManager.updateById(warehouseAreaTypeDO);
    }

    @ApiOperation(value = "查询库位类型")
    @GetMapping("/listAreaType")
    public List<WarehouseAreaTypeDO> listAreaType(Long companyId) {
        AdminUserContext.hasSuperAdminQuery(companyId);
        return warehouseAreaTypeManager.list(new LambdaQueryWrapper<WarehouseAreaTypeDO>()
                .eq(WarehouseAreaTypeDO::getCompanyId, AdminUserContext.getCompanyId()));
    }

    @ApiOperation(value = "根据仓库id获取所有的库存列表")
    @GetMapping("/goodNotAreaList/{warehouseId}")
    @Log(client = LogClient.admin, detail = "创建库位信息")
    public Page<ProductStockVO> goodNotAreaList(BaseQueryParam baseQueryParam, @PathVariable Long warehouseId) {
        return warehouseAreaManager.goodNotAreaList(baseQueryParam, warehouseId);
    }

    @ApiOperation(value = "根据仓库id获取所有的库位列表")
    @GetMapping("/getAreaByWarehouse/{warehouseId}")
    @Log(client = LogClient.admin, detail = "创建库位信息")
    public List<WarehouseAreaDO> getAreaByWarehouse(@PathVariable Long warehouseId) {
        return warehouseAreaManager.getAreaByWarehouse(warehouseId);
    }

    @ApiOperation(value = "新增关系")
    @PostMapping("/bindAreaStock/{warehouseId}/{warehouseAreaId}")
    @Log(client = LogClient.admin, detail = "创建库位信息")
    public void bindAreaStock(@PathVariable Long warehouseId, @PathVariable(value = "warehouseAreaId") Long warehouseAreaId, @RequestBody WarehouseAreaDO warehouseAreaDO) {
        AdminUserContext.hasCompany();
        warehouseAreaManager.bindAreaStock(warehouseId, warehouseAreaId, warehouseAreaDO.getStockIdList());
    }
}

