package cn.ezhizao.project.business.workshop.controller;

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

import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.domain.server.Sys;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.workshop.domain.BizWorkshop;
import cn.ezhizao.project.business.workshop.service.IBizWorkshopService;
import cn.ezhizao.project.system.domain.SysDept;
import cn.ezhizao.project.system.service.ISysDeptService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
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;


/**
 * 车间管理Controller
 *
 * @author ezhizao
 * @date 2024-02-22
 */
@RestController
@RequestMapping("/business/workshop")
public class BizWorkshopController extends BaseController
{
    @Resource
    private IBizWorkshopService bizWorkshopService;
    @Resource
    private ISysDeptService sysDeptService;

    /**
     * 查询车间管理列表
     */
//    @PreAuthorize("@ss.hasPermi('business:workshop:list')")
    @Log(title = "车间管理查询", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BizWorkshop bizWorkshop) throws NoSuchFieldException, IllegalAccessException
    {
//        startPage();
        List<BizWorkshop> list = bizWorkshopService.getList(bizWorkshop);
        return getDataTable(list);
    }

    /**
     * 导出车间管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:workshop:export')")
    @Log(title = "车间管理导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizWorkshop bizWorkshop) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizWorkshop);
        List<BizWorkshop> list = bizWorkshopService.getList(bizWorkshop);
        ExcelUtil<BizWorkshop> util = new ExcelUtil<BizWorkshop>(BizWorkshop.class);
        util.exportExcel(response, list, "车间管理数据");
    }

    /**
     * 获取车间管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:workshop:query')")
    @Log(title = "车间管理查询详细信息", businessType = BusinessType.EXPORT)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizWorkshopService.getById(id));
    }

    /**
     * 新增车间管理
     */
    @PreAuthorize("@ss.hasPermi('business:workshop:add')")
    @Log(title = "车间管理新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizWorkshop bizWorkshop) throws NoSuchFieldException, IllegalAccessException
    {
        QueryWrapper<BizWorkshop> queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",bizWorkshop.getName());
        queryWrapper.eq("deleted",0);
        if(bizWorkshopService.list(queryWrapper).size()>0){
            return error("车间名称已存在");
        }
        return toAjax(bizWorkshopService.save(bizWorkshop));
    }

    /**
     * 修改车间管理
     */
    @PreAuthorize("@ss.hasPermi('business:workshop:edit')")
    @Log(title = "车间管理修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizWorkshop bizWorkshop) throws NoSuchFieldException, IllegalAccessException
    {
        QueryWrapper<BizWorkshop> queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",bizWorkshop.getName());
        queryWrapper.ne("id",bizWorkshop.getId());
        queryWrapper.eq("deleted",0);
        if(bizWorkshopService.list(queryWrapper).size()>0){
            return error("车间名称已存在");
        }
        return toAjax(bizWorkshopService.updateById(bizWorkshop));
    }

    /**
     * 删除车间管理
     */
    @PreAuthorize("@ss.hasPermi('business:workshop:remove')")
    @Log(title = "车间管理删除", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper();
        queryWrapper.eq("workshop_id",ids.get(0));
        List<SysDept> sysDeptList = sysDeptService.selectDeptListByWorkshopId(ids.get(0));
        if(sysDeptList.size()>0) {
            return error("该车间下存在部门，不能删除");
        }
        return toAjax(bizWorkshopService.removeBatchByIds(ids));
    }
}
