package com.yiqi.web.controller.system;

import java.util.List;

import com.yiqi.common.core.domain.R;
import com.yiqi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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.yiqi.common.annotation.Log;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.enums.BusinessType;
import com.yiqi.system.domain.ManageFactory;
import com.yiqi.system.service.IManageFactoryService;
import com.yiqi.common.core.page.TableDataInfo;

/**
 * 工厂 基本信息Controller
 *
 * @author ruoyi
 * @date 2023-07-20
 */
@Api(tags = "工厂管理")
@RestController
@RequestMapping("/system/factory")
public class ManageFactoryController extends BaseController {
    @Autowired
    private IManageFactoryService yiqiManageFactoryService;

    /**
     * 查询工厂 基本信息列表
     */
    @ApiOperation("查询工厂列表")
    @PreAuthorize("@ss.hasPermi('system:factory:list')")
    @GetMapping("/list")
    public TableDataInfo list(ManageFactory manageFactory) {
        startPage();
        List<ManageFactory> list = yiqiManageFactoryService.selectYiqiManageFactoryList(manageFactory);
        return getDataTable(list);
    }

    /**
     * 获取工厂 基本信息详细信息
     */
    @ApiOperation("获取工厂明细")
    @PreAuthorize("@ss.hasPermi('system:factory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(yiqiManageFactoryService.selectYiqiManageFactoryById(id));
    }

    /**
     * 新增工厂 基本信息
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("新增工厂")
    @PreAuthorize("@ss.hasPermi('system:factory:add')")
    @Log(title = "工厂 基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ManageFactory manageFactory) {
        return toAjax(yiqiManageFactoryService.insertYiqiManageFactory(manageFactory));
    }

    /**
     * 修改工厂 基本信息
     */
    @ApiOperation("修改工厂")
    @PreAuthorize("@ss.hasPermi('system:factory:edit')")
    @Log(title = "工厂 基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ManageFactory manageFactory) {
        return toAjax(yiqiManageFactoryService.updateYiqiManageFactory(manageFactory));
    }

    /**
     * 删除工厂 基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:factory:remove')")
    @Log(title = "工厂 基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(yiqiManageFactoryService.deleteYiqiManageFactoryByIds(ids));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:factory:edit')")
    @Log(title = "工厂管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody ManageFactory factory) {
        factory.setUpdateBy(getUsername());
        return toAjax(yiqiManageFactoryService.updateFactoryStatus(factory));
    }

    /**
     * 获取工厂 基本信息详细信息
     */
    @ApiOperation("获取工厂明细")
    @GetMapping(value = "/getFactoryInfo")
    public R getFactoryInfo() {
        return R.ok(yiqiManageFactoryService.getById(SecurityUtils.getLoginUser().getOrgId()));
    }
}
