package com.shkj.web.controller.system;

import com.shkj.common.annotation.Log;
import com.shkj.common.core.controller.BaseController;
import com.shkj.common.core.domain.AjaxResult;
import com.shkj.common.core.domain.Result;
import com.shkj.common.core.domain.entity.BaseBranch;
import com.shkj.common.core.page.BaseBranchResultVo;
import com.shkj.common.core.page.BaseBranchVo;
import com.shkj.common.core.page.TableDataInfo;
import com.shkj.common.enums.BusinessType;
import com.shkj.common.utils.poi.ExcelUtil;
import com.shkj.system.service.IBaseBranchService;
import com.shkj.web.controller.common.SwitchStatusEntity;
import com.shkj.wms.service.IBaseConsignorService;
import com.shkj.wms.vo.BranchTree;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 仓库信息Controller
 *
 * @author lch
 * @menu 仓库信息管理
 * @date 2022-03-28
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/branch")
public class BaseBranchController extends BaseController {

    private final IBaseBranchService iBaseBranchService;
    private final IBaseConsignorService iBaseConsignorService;

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

    /**
     * 获取仓库列表
     */
    @PreAuthorize("@ss.hasPermi('system:branch:list')")
    @GetMapping("/list")
    public TableDataInfo<BaseBranchResultVo> list(BaseBranch branch) {
        startPage();
        List<BaseBranchResultVo> list = iBaseBranchService.queryList(branch);
        return getDataTable(list);
    }

    @Log(title = "仓库管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:branch:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response, BaseBranch Branch) {
        List<BaseBranchResultVo> list = iBaseBranchService.queryList(Branch);
        ExcelUtil<BaseBranchResultVo> util = new ExcelUtil<BaseBranchResultVo>(BaseBranchResultVo.class);
        util.exportExcel(response, list, "仓库数据");
    }

    /**
     * 根据仓库编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:branch:query')")
    @GetMapping(value = "/{branchId}")
    public Result<BaseBranch> getInfo(@PathVariable Long branchId) {
        return iBaseBranchService.selectBranchById(branchId);
    }

    /**
     * 新增仓库
     */
    @PreAuthorize("@ss.hasPermi('system:branch:add')")
    @Log(title = "仓库管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result add(@Validated @RequestBody BaseBranch Branch) {
        return iBaseBranchService.insertBranch(Branch);
    }

    /**
     * 修改仓库
     */
    @PreAuthorize("@ss.hasPermi('system:branch:edit')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Result edit(@Validated @RequestBody BaseBranch branch) {
        return iBaseBranchService.updateBranch(branch);
    }

    /**
     * 删除仓库
     */
    @PreAuthorize("@ss.hasPermi('system:branch:remove')")
    @Log(title = "仓库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{branchIds}")
    public Result remove(@PathVariable Long[] branchIds) {
        return iBaseBranchService.deleteBranchByIds(branchIds);
    }

    /**
     * 获取仓库选择框列表
     */
    @GetMapping("/optionSelect")
    public Result<List<BaseBranch>> optionSelect() {
        return iBaseBranchService.selectBranchAll();
    }

    /**
     * 登陆时根据输入的用户名获取仓库选择框列表
     */
    @GetMapping("/optionBranchByUserName/{userName}")
    public Result<BaseBranchVo> optionBranchByUserName(@PathVariable String userName) {
        return iBaseBranchService.optionBranchByUserName(userName);
    }

    /**
     * pda登陆时根据输入的用户名获取仓库选择框列表
     */
    @GetMapping("/pda/optionBranchByUserName/{userName}")
    public Result<BaseBranchVo> pdaOptionBranchByUserName(@PathVariable String userName) {
        return iBaseBranchService.pdaOptionBranchByUserName(userName);
    }

    /**
     * 加载对应角色货主树
     */
    @GetMapping(value = "/roleConsignorTreeSelect/{roleId}")
    public AjaxResult roleConsignorTreeSelect(@PathVariable("roleId") Long roleId) {
        List<BranchTree> treeList = iBaseConsignorService.selectBranchConsignor();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", iBaseConsignorService.selectConsignorListByRoleId(roleId));
        ajax.put("consignors", iBaseConsignorService.buildTreeSelect(treeList));
        return ajax;
    }
}
