package com.jeeplus.psimanage.supplier.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import com.jeeplus.psimanage.supplier.domain.PsiMaterialSupplier;
import com.jeeplus.psimanage.supplier.service.PsiMaterialSupplierService;
import com.jeeplus.psimanage.supplier.service.dto.PsiMaterialSupplierDTO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api("供货商管理")
@RestController
@RequestMapping(value = "/psi/supplier")
public class PsiMaterialSupplierController {

    @Autowired
    private PsiMaterialSupplierService materialSupplierService;

    /**
     * 查询供应商列表
     * @param materialSupplierDTO
     * @param page
     * @return
     * @throws Exception
     */
    @ApiLog("查询供应商列表")
//    @PreAuthorize("hasAuthority('material:supplier:list')")
    @GetMapping("list")
    public ResponseEntity<IPage<PsiMaterialSupplierDTO>> data(PsiMaterialSupplierDTO materialSupplierDTO, Page<PsiMaterialSupplierDTO> page) throws Exception {
        IPage<PsiMaterialSupplierDTO> result = new Page<PsiMaterialSupplierDTO>();
        result = materialSupplierService.findList (page, materialSupplierDTO);
        return ResponseEntity.ok (result);
    }

    /**
     * 查询供应商详情
     * @param id
     * @return
     */
    @ApiLog("查询供应商详情")
    @PreAuthorize ("hasAnyAuthority('psi:supplier:view','psi:supplier:add','psi:supplier:edit')")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam("id") String id) {
        PsiMaterialSupplierDTO materialSupplierDTO = materialSupplierService.queryById ( id );
        return ResponseEntity.ok (materialSupplierDTO);
    }

    /**
     * 保存供应商数据
     * @param materialSupplierDTO
     * @return
     */
    @ApiLog(value = "保存供应商数据", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('psi:supplier:add','psi:supplier:edit')")
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody PsiMaterialSupplierDTO materialSupplierDTO) throws Exception {
        materialSupplierService.saveSupplier(materialSupplierDTO);
        return ResponseEntity.ok ("操作成功");
    }

    /**
     * 删除供应商
     * @param ids
     * @return
     */
    @ApiLog(value = "删除供应商", type = LogTypeEnum.SAVE)
    @PreAuthorize ("hasAuthority('psi:supplier:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids) {
        return materialSupplierService.deleteByIds(ids);
    }

    /**
     * 根据名称查询供应商
     * @param name
     * @return
     */
    @ApiLog("根据名称查询供应商")
    @GetMapping("findByName")
    public ResponseEntity findByName(@RequestParam("name") String name) {
        PsiMaterialSupplier materialSupplier = materialSupplierService.findByName ( name );
        return ResponseEntity.ok (materialSupplier);
    }
}
