package cn.iocoder.yudao.module.inspection.controller;

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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.inspection.service.AppInspectionPartnerService;
import cn.iocoder.yudao.module.shop.entity.ShopMallPartners;
import cn.iocoder.yudao.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.iocoder.yudao.module.core.controller.BaseController;
import cn.iocoder.yudao.module.inspection.entity.InspectionFile;
import cn.iocoder.yudao.module.inspection.service.IInspectionFileService;
import cn.iocoder.yudao.module.core.page.TableDataInfo;

/**
 * inspectionFileController
 *
 * @author zcy
 * @date 2023-10-13
 */
@RestController
@RequestMapping("/admin-api/inspectionFile/inspectionFile")
public class InspectionFileController extends BaseController
{
    @Autowired
    private IInspectionFileService inspectionFileService;
    @Autowired
    private AppInspectionPartnerService partnerService;

    /**
     * 查询inspectionFile列表
     */
    @GetMapping("/list")
    public TableDataInfo list(InspectionFile inspectionFile) throws Exception {
        ShopMallPartners partners = partnerService.shopInfoByUserId();
        inspectionFile.setPartnerId(partners.getPartnerId());
        startPage();
        List<InspectionFile> list = inspectionFileService.selectInspectionFileList(inspectionFile);
        return getDataTable(list);
    }

    /**
     * 导出inspectionFile列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, InspectionFile inspectionFile)
    {
        List<InspectionFile> list = inspectionFileService.selectInspectionFileList(inspectionFile);
        ExcelUtil<InspectionFile> util = new ExcelUtil<InspectionFile>(InspectionFile.class);
        util.exportExcel(response, list, "inspectionFile数据");
    }

    /**
     * 获取inspectionFile详细信息
     */
    @GetMapping(value = "/{id}")
    public CommonResult getInfo(@PathVariable("id") Long id)
    {
        return success(inspectionFileService.selectInspectionFileById(id));
    }

    /**
     * 新增inspectionFile
     */
    @PostMapping("/add")
    public CommonResult add(@RequestBody InspectionFile inspectionFile) throws Exception {
        ShopMallPartners partners = partnerService.shopInfo();
        inspectionFile.setPartnerId(partners.getPartnerId());
        return toAjax(inspectionFileService.insertInspectionFile(inspectionFile));
    }

    /**
     * 修改inspectionFile
     */
    @PostMapping("/edit")
    public CommonResult edit(@RequestBody InspectionFile inspectionFile) throws Exception {
        ShopMallPartners partners = partnerService.shopInfo();
        InspectionFile file = inspectionFileService.selectInspectionFileById(inspectionFile.getId());
        if (!partners.getPartnerId().equals(file.getPartnerId())){
            return null;
        }
        return toAjax(inspectionFileService.updateInspectionFile(inspectionFile));
    }

    /**
     * 删除inspectionFile
     */
    @PostMapping("/del/{id}")
    public CommonResult remove(@PathVariable Long id) throws Exception {
        ShopMallPartners partners = partnerService.shopInfo();
        InspectionFile file = inspectionFileService.selectInspectionFileById(id);
        if (!partners.getPartnerId().equals(file.getPartnerId())){
            return null;
        }
        return toAjax(inspectionFileService.deleteInspectionFileById(id));
    }
}
