package com.aucma.base.controller;

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

import com.aucma.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
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.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
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 com.aucma.common.annotation.Log;
import com.aucma.common.core.controller.BaseController;
import com.aucma.common.core.domain.AjaxResult;
import com.aucma.common.enums.BusinessType;
import com.aucma.base.domain.BaseFactory;
import com.aucma.base.service.IBaseFactoryService;
import com.aucma.common.utils.poi.ExcelUtil;
import com.aucma.common.core.page.TableDataInfo;

/**
 * 工厂管理Controller
 *
 * @author Yinq
 * @date 2023-09-18
 */
@RestController
@RequestMapping("/base/factory")
public class BaseFactoryController extends BaseController {
    @Autowired
    private IBaseFactoryService baseFactoryService;

    /**
     * 查询工厂管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:factory:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseFactory baseFactory) {
        startPage();
        List<BaseFactory> list = baseFactoryService.selectBaseFactoryList(baseFactory);
        return getDataTable(list);
    }

    /**
     * 查询工厂管理列表（不分页）
     */
    @PreAuthorize("@ss.hasPermi('base:factory:list')")
    @GetMapping("/all")
    public AjaxResult all(BaseFactory baseFactory) {
        return success(baseFactoryService.selectBaseFactoryList(baseFactory));
    }

    /**
     * 查询工厂下拉框列表
     * @param baseFactory
     * @return
     */
    @GetMapping("/findFactoryList")
    public AjaxResult findFactoryList(BaseFactory baseFactory) {
        // 为什么保留旧接口：前端多个页面已在用该地址，这里继续复用全量查询能力，避免为了接口标准化引入联动回归。
        return all(baseFactory);
    }

    /**
     * 导出工厂管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:factory:export')")
    @Log(title = "工厂管理" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseFactory baseFactory) {
        List<BaseFactory> list = baseFactoryService.selectBaseFactoryList(baseFactory);
        ExcelUtil<BaseFactory> util = new ExcelUtil<BaseFactory>(BaseFactory.class);
        util.exportExcel(response, list, "工厂管理数据");
    }

    /**
     * 获取工厂管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:factory:query')")
    @GetMapping(value = "/{objId}")
    public AjaxResult getInfo(@PathVariable("objId") Long objId) {
        return success(baseFactoryService.selectBaseFactoryByObjId(objId));
    }

    /**
     * 新增工厂管理
     */
    @PreAuthorize("@ss.hasPermi('base:factory:add')")
    @Log(title = "工厂管理" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseFactory baseFactory) {
        baseFactory.setCreatedBy(getUsername());
        baseFactory.setCreatedTime(DateUtils.getNowDate());
        return toAjax(baseFactoryService.insertBaseFactory(baseFactory));
    }

    /**
     * 修改工厂管理
     */
    @PreAuthorize("@ss.hasPermi('base:factory:edit')")
    @Log(title = "工厂管理" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseFactory baseFactory) {
        baseFactory.setUpdatedBy(getUsername());
        baseFactory.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(baseFactoryService.updateBaseFactory(baseFactory));
    }

    /**
     * 删除工厂管理
     */
    @PreAuthorize("@ss.hasPermi('base:factory:remove')")
    @Log(title = "工厂管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{objIds}")
    public AjaxResult remove(@PathVariable Long[] objIds) {
        return toAjax(baseFactoryService.deleteBaseFactoryByObjIds(objIds));
    }
}
