package com.bonus.hn.bearing.capacity.backstage.manager.controller;
import com.bonus.hn.bearing.capacity.backstage.manager.annotation.LogAnnotation;
import com.bonus.hn.bearing.capacity.backstage.manager.entity.OrgBean;
import com.bonus.hn.bearing.capacity.backstage.manager.entity.ZNode;
import com.bonus.hn.bearing.capacity.backstage.manager.service.OrgService;
import com.bonus.hn.bearing.capacity.backstage.manager.service.ProjectService;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.AjaxRes;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.GlobalConst;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
/**
 * @author bonus
 */
@Api(tags = "组织管理")
@RestController
@RequestMapping("/org")
public class OrgController {

    @Autowired
    private OrgService service;


    @Resource(name = "ProjectService")
    private ProjectService proService;

    @LogAnnotation
    @PostMapping("getOrgList")
    @ApiOperation(value = "获取组织信息")
    public AjaxRes getOrgList(){
        AjaxRes ar = new AjaxRes();
        try {
            String orgId = UserUtil.getLoginUser().getOrgId();
            String companyId = proService.getCompanyId(orgId);
            List<ZNode> list= service.getAll("");
            for(int i=0;i<list.size();i++){
                list.get(i).setOpen(true);
            }
            ar.setSucceed(list);
        } catch (Exception e) {
             e.printStackTrace();
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;

    }

    @LogAnnotation
    @PostMapping("getCompanyTreeList")
    @ApiOperation(value = "获取组织信息")
    public AjaxRes getCompanyTree(OrgBean bean){
        AjaxRes ar = new AjaxRes();
        try {
            String orgId = UserUtil.getLoginUser().getOrgId();
            String companyId = proService.getCompanyId(orgId);
            if (companyId == null || companyId == ""){
                companyId = "0";
            }
            bean.setCompanyId(Integer.valueOf(companyId));
            List<ZNode> list= service.getCompanyTree(bean);
            ar.setSucceed(list);
        } catch (Exception e) {

            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;

    }

    @LogAnnotation
    @PostMapping("deleteOrg")
    @ApiOperation(value = "删除组织")
    public AjaxRes deleteOrg( OrgBean bean){
        AjaxRes ar = new AjaxRes();
        int result = service.deleteOrg(bean);
        if (result == 1){
            ar.setSucceed(null, GlobalConst.DEL_SUCCEED);
        }else {
            ar.setSucceed(null, GlobalConst.DEL_FAIL);
        }
        return ar;
    }

    @LogAnnotation
    @PostMapping("updateOrg")
    @ApiOperation(value = "修改组织")
    public AjaxRes updateOrg( OrgBean bean){
        AjaxRes ar = new AjaxRes();
        int result = service.updateOrg(bean);
        if (result == 1){
            ar.setSucceed(null, GlobalConst.SAVE_SUCCEED);
        }else {
            ar.setSucceed(null, GlobalConst.SAVE_FAIL);
        }
        return ar;
    }

    @LogAnnotation
    @PostMapping("/addOrg")
    @ApiOperation(value = "添加组织")
    public AjaxRes addOrg( OrgBean bean){
        AjaxRes ar = new AjaxRes();
        int result = service.addOrg(bean);
        if (result == 1){
            ar.setSucceed(null, GlobalConst.SAVE_SUCCEED);
        }else {
            ar.setSucceed(null, GlobalConst.SAVE_FAIL);
        }
        return ar;
    }
}
