package com.qs.mp.web.controller.api.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qs.mp.admin.domain.Goods;
import com.qs.mp.admin.domain.Supplier;
import com.qs.mp.admin.domain.param.SupplierCreateParam;
import com.qs.mp.admin.domain.param.SupplierQueryParam;
import com.qs.mp.admin.domain.param.SupplierUpdateParam;
import com.qs.mp.admin.service.IGoodsService;
import com.qs.mp.admin.service.ISupplierService;
import com.qs.mp.common.core.domain.AjaxResult;
import com.qs.mp.common.core.page.TableDataInfo;
import com.qs.mp.web.controller.common.BaseApiController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
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.*;

/**
 * 供应商管理API
 *
 * @author Cup
 * @date 2022/5/10
 */
@Api(tags = "供应商管理API")
@RestController
@RequestMapping("/api/v1/mp/admin/supplier/*")
public class SupplierMgrController extends BaseApiController {

    @Autowired
    private ISupplierService supplierService;

    @Autowired
    private IGoodsService goodsService;


    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermi('business:supplier:add')")
    @ApiOperation("添加供应商")
    public AjaxResult createSupplier(@RequestBody @Validated SupplierCreateParam supplierCreateParam) {
        int count = supplierService.count(new LambdaQueryWrapper<Supplier>().eq(Supplier::getName, supplierCreateParam.getName()));
        if (count != 0) {
            return AjaxResult.error("供应商名称已存在");
        }
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierCreateParam, supplier);
        return AjaxResult.success(supplierService.save(supplier));
    }

    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('business:supplier:edit')")
    @ApiOperation("编辑供应商")
    public AjaxResult updateSupplier(@RequestBody @Validated SupplierUpdateParam supplierUpdateParam) {
        int count = supplierService.count(new LambdaQueryWrapper<Supplier>().eq(Supplier::getName, supplierUpdateParam.getName()).notIn(Supplier::getId, supplierUpdateParam.getId()));
        if (count != 0) {
            return AjaxResult.error("供应商名称已存在");
        }
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierUpdateParam, supplier);
        return AjaxResult.success(supplierService.updateById(supplier));
    }

    @PostMapping("/list")
    @PreAuthorize("@ss.hasPermi('business:supplier:list')")
    @ApiOperation("供应商列表")
    @ApiResponses(
            @ApiResponse(code = 200, message = "供应商列表", response = Supplier.class)
    )
    public TableDataInfo list(@RequestBody SupplierQueryParam supplierQueryParam) {
        startPage();

        LambdaQueryWrapper<Supplier> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(supplierQueryParam.getName())) {
            queryWrapper.like(Supplier::getName, supplierQueryParam.getName());
        }

        queryWrapper.orderByDesc(Supplier::getId);

        return getDataTable(supplierService.list(queryWrapper));
    }

    @PostMapping("/detail/{id}")
    @PreAuthorize("@ss.hasPermi('business:supplier:query')")
    @ApiOperation("供应商详情")
    @ApiResponses(
            @ApiResponse(code = 200, message = "供应商详情", response = Supplier.class)
    )
    public AjaxResult detail(@PathVariable("id") Long id) {
        return AjaxResult.success(supplierService.getById(id));
    }

    @PostMapping("/delete/{id}")
    @PreAuthorize("@ss.hasPermi('business:supplier:remove')")
    @ApiOperation("删除供应商")
    public AjaxResult delete(@PathVariable("id") Long id) {
        int count = goodsService.count(new LambdaQueryWrapper<Goods>().eq(Goods::getSupplierId, id));
        if (count != 0) {
            return AjaxResult.error("该供应商下有商品，不能删除");
        }
        return AjaxResult.success(supplierService.removeById(id));
    }

}
