package com.jeeplus.test.materialManagement.supplier.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.sys.constant.enums.LogTypeEnum;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.cw.invoice.service.dto.CwFinanceInvoiceDTO;
import com.jeeplus.test.materialManagement.supplier.domain.MaterialSupplier;
import com.jeeplus.test.materialManagement.supplier.service.MaterialSupplierService;
import com.jeeplus.test.materialManagement.supplier.service.dto.MaterialSupplierDTO;
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 = "/material/supplier")
public class MaterialSupplierController {

    @Autowired
    private MaterialSupplierService materialSupplierService;

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

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

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

    /**
     * 删除供应商
     * @param ids
     * @return
     */
    @ApiLog(value = "删除供应商", type = LogTypeEnum.SAVE)
    @PreAuthorize ("hasAuthority('material: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) {
        MaterialSupplier materialSupplier = materialSupplierService.findByName ( name );
        return ResponseEntity.ok (materialSupplier);
    }
}
