package com.ruoyi.project.base.controller;

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

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.service.ISysDeptService;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.base.domain.TBasePlant;
import com.ruoyi.project.base.service.ITBasePlantService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 装置Controller
 *
 * @author ruoyi
 * @date 2022-11-14
 */
@RestController
@RequestMapping("/base/plant")
public class TBasePlantController extends BaseController
{
    @Autowired
    private ITBasePlantService tBasePlantService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询装置列表
     */
    @PreAuthorize("@ss.hasPermi('base:plant:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBasePlant tBasePlant)
    {
        startPage();
        List<TBasePlant> list = tBasePlantService.selectTBasePlantList(tBasePlant);
        return getDataTable(list);
    }

    @GetMapping("/allPlantName")
    public AjaxResult selectAllPlantName()
    {
        return AjaxResult.success(tBasePlantService.selectAllPlantName());
    }

    /**
     * 导出装置列表
     */
    @PreAuthorize("@ss.hasPermi('base:plant:export')")
    @Log(title = "装置导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBasePlant tBasePlant)
    {
        List<TBasePlant> list = tBasePlantService.selectTBasePlantList(tBasePlant);
        ExcelUtil<TBasePlant> util = new ExcelUtil<TBasePlant>(TBasePlant.class);
        util.exportExcel(response, list, "装置数据");
    }

    /**
     * 获取装置详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:plant:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tBasePlantService.selectTBasePlantById(id));
    }

    /**
     * 新增装置
     */
    @PreAuthorize("@ss.hasPermi('base:plant:add')")
    @Log(title = "装置新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBasePlant tBasePlant)
    {
        TBasePlant plant = tBasePlantService.selectTBasePlantByName(tBasePlant.getPlantName());
        if (plant != null){
            return AjaxResult.success("当前装置已存在!");
        }
        tBasePlant.setUpdatedate(new Date());
        tBasePlant.setUpdaterCode(getUserId());
        tBasePlant.setCreaterCode(getUserId());
        return toAjax(tBasePlantService.insertTBasePlant(tBasePlant));
    }

    /**
     * 修改装置
     */
    @PreAuthorize("@ss.hasPermi('base:plant:edit')")
    @Log(title = "装置修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBasePlant tBasePlant)
    {
        tBasePlant.setUpdatedate(new Date());
        tBasePlant.setUpdaterCode(getUserId());
        return toAjax(tBasePlantService.updateTBasePlant(tBasePlant));
    }

    /**
     * 删除装置
     */
    @PreAuthorize("@ss.hasPermi('base:plant:remove')")
    @Log(title = "装置删除", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tBasePlantService.deleteTBasePlantByIds(ids));
    }

    @PutMapping("/handleApprove")
    @Log(title = "装置审核", businessType = BusinessType.APPROVE)
    public AjaxResult handleApprove(@RequestBody TBasePlant tBasePlant){
        tBasePlant.setApproveTime(new Date());
        int i = tBasePlantService.updateTBasePlantByPlantIds(tBasePlant);
        if (i >0){
            for (Long plantId : tBasePlant.getPlantIds()) {
                TBasePlant plant = tBasePlantService.selectTBasePlantById(plantId);
                List<SysDept> sysDepts = deptService.selectDeptList(new SysDept());
                SysDept dept = new SysDept();
                dept.setDeptId(plantId);
                dept.setParentId(100L);
                dept.setDeptName(plant.getPlantName());
                dept.setOrderNum(sysDepts.size());
                dept.setStatus("0");
                if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
                {
                    return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败，部门名称已存在");
                }
                dept.setCreateBy(getUsername());
                deptService.insertDept(dept);
            }

        }
        return AjaxResult.success();
    }
}
