package com.yanzhu.web;

import com.yanzhu.common.annotation.Log;
import com.yanzhu.common.core.controller.BaseController;
import com.yanzhu.common.core.domain.AjaxResult;
import com.yanzhu.common.core.domain.entity.SysDept;
import com.yanzhu.common.enums.BusinessType;
import com.yanzhu.framework.web.domain.server.Sys;
import com.yanzhu.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/project/company")
public class CompanyController extends BaseController {
    @Autowired
    private ISysDeptService deptService;


    @PreAuthorize("@ss.hasAnyPermi('project:company:list')")
    @PostMapping("/list")
    public AjaxResult companyList(@RequestBody SysDept where)
    {
        where.setParentId(100l);
        where.setDelFlag("0");
        List<SysDept> depts = deptService.selectDeptList(where);
        return success(depts);
    }

    @PreAuthorize("@ss.hasAnyPermi('project:subCompany:list,project:project:list')")
    @PostMapping("/subList")
    public AjaxResult subList(@RequestBody SysDept where){
        where.setDelFlag("0");
        List<SysDept> depts = deptService.selectDeptList(where);
        return success(depts);
    }
    @PreAuthorize("@ss.hasAnyPermi('project:subCompany:delete')")
    @Log(title = "段队管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptId}")
    public AjaxResult remove(@PathVariable Long deptId) {
        SysDept where=new SysDept();
        where.setParentId(deptId);
        where.setLevel(300);
        where.setDelFlag("0");
        List<SysDept> list=deptService.selectDeptList(where);
        if(list.size()>0){
            return  AjaxResult.error("段队下面有项目,不能删除!");
        }
        return toAjax(deptService.deleteDeepDeptById(deptId));
    }

    @PreAuthorize("@ss.hasAnyPermi('project:subCompany:add')")
    @Log(title = "段队管理", businessType = BusinessType.INSERT)
    @PostMapping("/addSubCompany")
    public AjaxResult addSubCompany(@RequestBody SysDept dept){
        if (!deptService.checkDeptNameUnique(dept))
        {
            return error("新增部门'" + dept.getDeptName() + "'失败，段队名称已存在");
        }
        dept.setCreateBy(getUsername());
        return toAjax(deptService.insertSubCompany(dept));
    }
    @PreAuthorize("@ss.hasAnyPermi('project:project:add')")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping("/addSubProject")
    public AjaxResult addSubProject(@RequestBody SysDept dept){
        if (!deptService.checkDeptNameUnique(dept))
        {
            return error("新增部门'" + dept.getDeptName() + "'失败，项目名称已存在");
        }
        dept.setCreateBy(getUsername());
        return toAjax(deptService.inertSubProject(dept));
    }
}
