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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.core.controller.BaseController;
import cn.iocoder.yudao.module.inspection.entity.InspectionFile;
import cn.iocoder.yudao.module.inspection.service.AppInspectionPartnerService;
import cn.iocoder.yudao.module.inspection.service.IInspectionFileService;
import cn.iocoder.yudao.module.shop.entity.ShopMallPartners;
import cn.iocoder.yudao.util.ExcelUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 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 CommonResult list(InspectionFile inspectionFile,
                             @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                             @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize) throws Exception {
        ShopMallPartners partners = partnerService.shopInfoByUserId();
        inspectionFile.setPartnerId(partners.getPartnerId());

        Page<InspectionFile> page = new Page<>(pageNum, pageSize);

        // 使用MyBatis-Plus的分页查询，将结果转换为Page对象
        IPage<InspectionFile> list = inspectionFileService.selectInspectionFileList(page,inspectionFile);

        return success(list);
    }

    /**
     * 导出inspectionFile列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, InspectionFile inspectionFile,
                       @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                       @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize)
    {
        Page<InspectionFile> page = new Page<>(pageNum, pageSize);
        IPage<InspectionFile> list = inspectionFileService.selectInspectionFileList(page,inspectionFile);
        ExcelUtil<InspectionFile> util = new ExcelUtil<InspectionFile>(InspectionFile.class);
        util.exportExcel(response, list.getRecords(), "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));
    }
}
