CompanyController.java 5.31 KB
package com.huaheng.system.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huaheng.common.core.web.controller.BaseController;
import com.huaheng.common.core.web.domain.AjaxResult;
import com.huaheng.common.core.web.page.PageDomain;
import com.huaheng.common.core.web.page.TableDataInfo;
import com.huaheng.common.core.web.page.TableSupport;
import com.huaheng.common.log.annotation.Log;
import com.huaheng.common.log.enums.BusinessType;
import com.huaheng.common.security.annotation.PreAuthorize;
import com.huaheng.common.security.utils.SecurityUtils;
import com.huaheng.system.api.domain.Company;
import com.huaheng.system.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.management.PlatformManagedObject;
import java.util.List;

@RestController
@RequestMapping("/company")
@Api(value = "货主", tags = "货主")
public class CompanyController extends BaseController {

    @Resource
    private CompanyService companyService;

    @GetMapping("/companies")
    @ApiOperation(value = "根据用户id和仓库编码获取货主")
    public List<Company> queryByUserIdAndWare(Long userId, String warehouseCode) {
        return companyService.selectCompanyByCurrentUserId(userId, warehouseCode);
    }

    @GetMapping("/{code}")
    @ApiOperation(value = "根据用户id和仓库编码获取货主")
    public Company getCompanyByCode(@PathVariable String code) {
        LambdaQueryWrapper<Company> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Company::getCode, code);
        return companyService.getOne(queryWrapper);
    }

    @PreAuthorize(hasPermi = "config:company:list")
    @GetMapping("/list")
    @ResponseBody
    @ApiOperation(value = "获取货主列表")
    public TableDataInfo list(Company company, String createdBegin, String createdEnd) {
        LambdaQueryWrapper<Company> lambdaQueryWrapper = Wrappers.lambdaQuery();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        lambdaQueryWrapper.gt(StrUtil.isNotEmpty(createdBegin), Company::getCreated, createdBegin)
                .lt(StrUtil.isNotEmpty(createdEnd), Company::getCreated, createdEnd)
                .eq(Company::getWarehouseCode, SecurityUtils.getWarehouseCode())
                .like(StrUtil.isNotBlank(company.getCode()) && StrUtil.isNotEmpty(company.getCode()), Company::getCode, company.getCode())
                .like(StrUtil.isNotBlank(company.getCode()) && StrUtil.isNotEmpty(company.getName()), Company::getName, company.getName())
                .eq(Company::getDeleted, false)
                .orderByDesc(Company::getId);

        if (ObjectUtil.isNotNull(pageNum) && ObjectUtil.isNotNull(pageSize)) {
            /**
             * 使用分页查询
             */
            Page<Company> page = new Page<>(pageNum, pageSize);
            IPage<Company> iPage = companyService.page(page, lambdaQueryWrapper);
            return getMpDataTable(iPage.getRecords(), iPage.getTotal());
        } else {
            List<Company> list = companyService.list(lambdaQueryWrapper);
            return getDataTable(list);
        }
    }

    @PreAuthorize(hasAnyPermi = "config:company:add")
    @Log(title = "货主管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增货主")
    public AjaxResult addSave(Company company) {
        if (StrUtil.isEmpty(company.getCode()) && StrUtil.isEmpty(company.getName())) {
            return AjaxResult.error("货主编码和名称不能为空!");
        }
        company.setWarehouseCode(SecurityUtils.getWarehouseCode());
        return companyService.addCompany(company);
    }

    @ApiOperation(value = "修改货主")
    @PreAuthorize(hasAnyPermi = "config:company:edit")
    @Log(title = "配置-库存资料-货主管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult editSave(Company company) {
        return companyService.updateCompany(company);
    }

    /**
     * 删除货主
     */
    @PreAuthorize(hasAnyPermi = "config:company:remove")
    @Log(title = "配置-库存资料-货主管理", businessType = BusinessType.DELETE)
    @DeleteMapping
    @ApiOperation(value = "删除货主")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(@ApiParam(value = "货主id") @RequestParam String ids) {
        if (StrUtil.isEmpty(ids)) {
            return AjaxResult.error("id不能为空");
        }

        for (Integer id : Convert.toIntArray(ids)) {
            Company company = new Company();
            company.setId(id);
            company.setDeleted(true);
            companyService.updateById(company);
        }
        return AjaxResult.success("删除成功!");
    }
}