package com.yh.saas.platform.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yh.saas.common.support.entity.BaseModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.winsea.svc.base.base.entity.CommonRole;
import com.winsea.svc.base.base.service.ICommonRoleService;

/**
 * <p>
 * 岸端职务 - Controller
 * </p>
 *
 * @author ZhangMingYu
 * @since 2019年9月19日13:53:32
 */

@RestController
@RequestMapping("/role")
public class RoleController {

	@SofaReference
	private ICommonRoleService roleService;

	/**
	 * 通过部门获取职务列表
	 *
	 * @param commonRole deptId 部门id
	 * @return 分页职务
	 */
	@GetMapping("/query/role")
	public Page<CommonRole> role(CommonRole commonRole) {
		return roleService.selectPage(commonRole.getQueryPage(),
				new EntityWrapper<CommonRole>().eq(CommonRole.QueryFields.DEPT_ID, commonRole.getDeptId()));
	}

	/**
	 * 通过公司查询职务列表
	 *
	 * @param commonRole compId 公司id
	 * @return 返回集合
	 */
	@GetMapping("/query/roleList")
	public List<HashMap> roleList(CommonRole commonRole) {
		return roleService.roleList(commonRole);
	}

	/**
	 * 通过部门id 查询角色
	 */
	@GetMapping("/query/deptRoleList")
	public List<CommonRole> findDeptRoleList(@RequestParam String deptIds) {
		List<String> deptIdlist = new ArrayList<>();
		if (deptIds.contains(",")) {
			deptIdlist = Arrays.asList(deptIds.split(","));
		} else {
			deptIdlist.add(deptIds);
		}
		return roleService.findDeptRoleList(deptIdlist);
	}

	/**
	 * 根据船舶id 获取所有职务
	 * 
	 * @param vesselId 船舶id
	 * @return 返回所有职务
	 */
	@GetMapping("/query/findVesselRole")
	public List<CommonRole> findVesselRole(@RequestParam String vesselId) {
		return roleService.findVesselRole(vesselId);
	}


	@GetMapping("/query/getRoleByCompId")
	public List<CommonRole> getRoleByCompId(String compId,String flag) {
		Wrapper<CommonRole> wrapper = new EntityWrapper<>();
		wrapper.eq(CommonRole.QueryFields.COMP_ID, compId);
		wrapper.eq(CommonRole.QueryFields.VESSEL_BANK_FLAG, flag);
		wrapper.orderBy("duty_id IS NULL", true);// 把没有船端职务id的放后面
		wrapper.orderBy("duty_id = ''", true);
		wrapper.orderBy("CONVERT ( duty_id, signed )", true);
		wrapper.orderBy(BaseModel.CommonQueryFields.CREATE_DATE, false);
		return roleService.selectList(wrapper);// 按船长,大副,二副,三副,轮机长,大管轮,二管轮,三管轮,电机员排序
	}
}
