package cn.estsh.i3plus.core.apiservice.controller.busi;

import cn.estsh.i3plus.core.api.iservice.busi.ISysPositionService;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.tool.StringTool;
import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.platform.bean.SysPosition;
import cn.estsh.i3plus.pojo.platform.bean.SessionUser;
import cn.estsh.impp.framework.base.controller.CoreBaseController;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.exception.ImppExceptionEnum;
import cn.estsh.impp.framework.boot.util.ResultBean;
import cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description : 岗位信息
 * @Reference :
 * @Author : yunhao
 * @Date : 2018-10-19 17:05
 * @Modify :
 **/
@RestController
@Api(description = "岗位管理服务")
@RequestMapping(PlatformConstWords.BASE_URL +"/position")
public class SysPositionController extends CoreBaseController {
    public static final Logger LOGGER = LoggerFactory.getLogger(SysPositionController.class);

    @Autowired
    private ISysPositionService positionService;

    @PostMapping(value="/insert")
    @ApiOperation(value="添加岗位",notes="返回内容添加岗位")
    public ResultBean insertSysPosition(SysPosition position){
        try {
            //登陆用户
            SessionUser user = AuthUtil.getSessionUser();

            //条件验证
            ValidatorBean.beginValid(position)
                    .notNull("name",position.getName())
                    .notNull("positionCode",position.getPositionCode());

            ConvertBean.modelInitialize(position,user);
            if(position.getParentId() == null || position.getParentId() <= 0){
                position.setParentId(CommonEnumUtil.PARENT.DEFAULT.getValue());
                position.setParentNameRdd("顶级岗位");
            }

            SysPosition pos = positionService.insertSysPosition(position);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(pos);
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ResultBean.fail(e).setCode(ImppExceptionEnum.BUSINESS_EXCEPTION_DATA_ERROR.getCode());
        }
    }

    @PutMapping(value="/update")
    @ApiOperation(value="修改岗位",notes="根据岗位id修改信息")
    public ResultBean updateSysPositionById(SysPosition position){
        try {
            //登陆用户
            SessionUser user = AuthUtil.getSessionUser();

            //条件验证
            ValidatorBean.beginValid(position)
                    .notNull("id",position.getId())
                    .notNull("name",position.getName())
                    .notNull("positionCode",position.getPositionCode());

            //修改初始化
            ConvertBean.modelUpdate(position,user);
            if(position.getParentId() == null || position.getParentId() <= 0){
                position.setParentId(CommonEnumUtil.PARENT.DEFAULT.getValue());
                position.setParentNameRdd("顶级岗位");
            }

            positionService.updateSysPosition(position);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ResultBean.fail(e).setCode(ImppExceptionEnum.BUSINESS_EXCEPTION_DATA_ERROR.getCode());
        }
    }

    @GetMapping(value="/list")
    @ApiOperation(value="查询所有岗位",notes="查询所有岗位")
    public ResultBean findSysPositionAll(){
        try{
            List<SysPosition> positionList = positionService.listSysPosition();
            return ResultBean.success("操作成功").setResultList(positionList)
                                .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ResultBean.fail(e).setCode(ImppExceptionEnum.SYSTEM_EXCEPTION.getCode());
        }
    }

    @GetMapping(value="/query")
    @ApiOperation(value="查询岗位",notes="复杂查询岗位带分页")
    public ResultBean querySysPosition(SysPosition position,Pager pager){
        try{
            ListPager<SysPosition> listPager = positionService.querySysPosition(position, pager);
            return ResultBean.success("操作成功").setListPager(listPager)
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ResultBean.fail(e).setCode(ImppExceptionEnum.SYSTEM_EXCEPTION.getCode());
        }
    }

    @GetMapping(value="/get/{id}")
    @ApiOperation(value="根据id查询岗位",notes="根据id查询岗位")
    public ResultBean getSysPositionById(@PathVariable("id") String id){
        try{
            ValidatorBean.checkNotNull(id, "id 不能为空");

            SysPosition position = positionService.getSysPositionById(Long.parseLong(id));
            ValidatorBean.beginValid(position)
                            .notNull("id",position.getId());

            return ResultBean.success("操作成功").setResultObject(position).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ResultBean.fail(e).setCode(ImppExceptionEnum.SYSTEM_EXCEPTION.getCode());
        }
    }

    @DeleteMapping(value="/delete/{id}")
    @ApiOperation(value="删除岗位",notes="根据岗位id删除信息")
    public ResultBean deleteSysPosition(@PathVariable("id") String id){
        try {
            //条件验证
            ValidatorBean.checkNotNull(id,"id为空");

            positionService.deleteSysPositionById(Long.parseLong(id));
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ResultBean.fail(e).setCode(ImppExceptionEnum.BUSINESS_EXCEPTION_DATA_ERROR.getCode());
        }
    }

    @DeleteMapping(value = "/batch-delete")
    @ApiOperation(value = "根据id批量删除", notes = "根据id批量删除")
    public ResultBean deleteSysPositionByIds(String[] ids){
        try{
            ids = ConvertBean.modelSafeArrayNumber(ids,true);
            if(ids.length == 0){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要删除的资源。")
                        .build();
            }
            positionService.deleteSysPositionByIds(StringTool.getArrayLong(ids));
            return ResultBean.success("操作成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ResultBean.fail(e).setCode(ImppExceptionEnum.SYSTEM_EXCEPTION.getCode());
        }
    }
}
