package com.zcxk.controller;

import com.zcxk.dto.OrgBaseTreeInfoDto;
import com.zcxk.dto.OrgTree;
import com.zcxk.entity.Org;
import com.zcxk.logAdvice.LogAnnotation;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.LoginUser;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.OrgService;
import com.zcxk.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 组织(Org)控制层
 *
 * @author makejava
 * @since 2020-10-26 14:55:43
 */
@RestController
@RequestMapping("/org")
@Api(tags = "组织机构")
public class OrgController {
    /**
     * 服务对象
     */
    @Autowired
    private OrgService orgService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 参数对象
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.POST)
    @ApiOperation(value = "查询机构列表")
    public AjaxMessage<Org> selectOne(
            @ApiParam(value = "机构Id", required = true)@RequestParam Integer id) {
        Org org=new Org();
        org.setId(id);
        Org result = orgService.selectById(org.getId());

        return new AjaxMessage<>(ResultStatus.OK, result);
    }


    /**
     * 新增一条数据
     *
     * @param org 实体类
     * @return Response对象
     */
    @LogAnnotation(module = "【机构管理】新增")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ApiOperation(value = "插入机构",notes = "orgType 为company或department")
    public AjaxMessage<Integer> insert(@ApiParam(value = "设置配置", required = true) @RequestBody Org org) {

        int result = orgService.insert(org);

        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 修改一条数据
     *
     * @param org 实体类
     * @return Response对象
     */
    @LogAnnotation(module = "【机构管理】修改")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ApiOperation(value = "更改机构",notes = "orgType 为company或department")
    public AjaxMessage<Integer> update(@ApiParam(value = "设置配置", required = true) @RequestBody Org org) {
        int result = orgService.update(org);
        if(result==-1){
            return new AjaxMessage<>(ResultStatus.ORG_HAS_USER);
        }else if(result==-2){
            return new AjaxMessage<>(ResultStatus.ORG_CHIRDTYPE_ERROR);
        }else if(result==-3){
            return new AjaxMessage<>(ResultStatus.ORG_PARENT_ERROR);
        }else{
            return new AjaxMessage<>(ResultStatus.OK, result);
        }


    }





    /**
     * 获取机构树
     *
     * @param
     * @param
     * @return Response对象
     */
    @RequestMapping(value = "getOrgTree", method = RequestMethod.POST)
    @ApiOperation(value = "获取机构树")
    public AjaxMessage<List<OrgTree>> getOrgTree(@ApiParam(value = "机构类型", required = true)

                                                             @RequestParam  String orgType) {
        Org org=new Org();
        if("company".equals(orgType)) {
            org.setOrgType(orgType);
        }
        return new AjaxMessage<>(ResultStatus.OK, orgService.getTrees(org));
    }
    @LogAnnotation(module = "【机构管理】删除")
    @RequestMapping(value = "deleteAll", method = RequestMethod.POST)
    @ApiOperation(value = "删除机构")
    AjaxMessage<List<String>> deleteAll(@ApiParam(value = "机构id", required = true)@RequestBody List<Integer> ids){

        return new AjaxMessage<>(ResultStatus.OK,
                orgService.deleteAll(ids));
    }
    /**
     * 获取机构树
     *
     * @param
     * @param
     * @return Response对象
     */
    @RequestMapping(value = "getOrgUserTree", method = RequestMethod.POST)
    @ApiOperation(value = "获取机构树")
    public AjaxMessage<List<OrgTree>> getOrgUserTree(@ApiParam(value = "机构类型", required = true)

                                                 @RequestParam  String orgType, @RequestParam  Integer id) {
        Org org=new Org();
        org.setOrgType(orgType);
        org.setId(id);

        return new AjaxMessage<>(ResultStatus.OK, orgService.getUserTrees(org));
    }

    @RequestMapping(value = "getAllByTenantId", method = RequestMethod.POST)
    public List<Org> getAllByTenantId(){
        Org org=new Org();
        org.setStatus(1);
        LoginUser currentUser = UserUtil.getCurrentUser();
        org.setTenantId(currentUser.getTenantId());

        return orgService.selectList(org);
    }
    @RequestMapping(value = "getAll", method = RequestMethod.POST)
    public AjaxMessage<List<Org>> getAll(){
        Org org=new Org();
        org.setStatus(1);
        LoginUser currentUser = UserUtil.getCurrentUser();
        org.setTenantId(currentUser.getTenantId());
        org.setProgramItems(currentUser.getProgramItemList());
        org.setPermissonType(currentUser.getPermissonType());

        return new AjaxMessage<>(ResultStatus.OK, orgService.selectList(org)) ;
    }

    @RequestMapping(value = "countCompanyByUser", method = RequestMethod.POST)
    public Integer countCompanyByUser() {
        return orgService.countCompanyByUser();
    }


    @RequestMapping(value = "getCompanyByUser", method = RequestMethod.POST)
    public List<OrgBaseTreeInfoDto> getCompanyByUser(@RequestParam(value = "condition" ,required =  false) String condition,@RequestBody List<Integer> companyIds ) {
        return orgService.getCompanyByUser(condition,companyIds);
    }
}