package com.yh.saas.platform.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.winsea.svc.base.security.service.IAccountService;
import com.yh.saas.plugin.yiliangyiyun.entity.WarehouseBaseInfo;
import com.yh.saas.plugin.yiliangyiyun.service.IWarehouseBaseInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.mapper.Wrapper;
import com.winsea.svc.base.base.entity.CommonDepartment;
import com.winsea.svc.base.base.entity.CommonRole;
import com.winsea.svc.base.base.entity.CommonStaff;
import com.winsea.svc.base.base.entity.CommonStaffRole;
import com.winsea.svc.base.base.entity.CommonVessel;
import com.winsea.svc.base.base.service.ICommonDepartmentService;
import com.winsea.svc.base.base.service.ICommonRoleService;
import com.winsea.svc.base.base.service.ICommonStaffRoleService;
import com.winsea.svc.base.base.service.ICommonStaffService;
import com.winsea.svc.base.base.service.ICommonVesselService;
import com.winsea.svc.base.security.util.AuthSecurityUtils;

/**
 * <p>
 * 职员信息 前端控制器
 * </p>
 *
 * @author zhaojiuzhou
 * @since 2019-05-28
 */
@RestController
@RequestMapping("/staff")
public class StaffController {

	@SofaReference
	private ICommonStaffService staffService;

	@SofaReference
	private ICommonDepartmentService deptService;

	@SofaReference
	private ICommonVesselService vesselInfoService;

	@SofaReference
	private ICommonDepartmentService deptInfoService;

	@SofaReference
	private ICommonStaffRoleService staffRoleService;

	@SofaReference
	private ICommonRoleService roleService;

	@Autowired
	private IWarehouseBaseInfoService warehouseBaseInfoService;
	@Autowired
	private ICommonStaffService commonStaffService;
	@Autowired
	private ICommonStaffRoleService commonStaffRoleService;
	/**
	 * 获取当前登录用户信息
	 *
	 * @return
	 */
	@GetMapping("/query/getCurrentUserInfo")
	public CommonStaff getCurrentUserInfo() {
		String staffId = AuthSecurityUtils.getCurrentUserInfo().getUserId();
		CommonStaff staff = staffService.selectById(staffId);
		CommonDepartment dept = deptService.selectById(staff.getDeptId());
		if (dept != null) {
			staff.setDeptName(dept.getDeptName());
		}
		staff.setRoles(staffService.getStaffRoles(staffId));
		staff.setVessels(vesselInfoService.getStaffVessels(staffId, CommonVessel.VesselStatus.UNDER_CONTROL));
		CommonStaffRole staffRole = staffRoleService.getMajorRole(staffId);
		if (staffRole != null) {
			staff.setMajorRole(roleService.selectById(staffRole.getRoleId()));
		}
		return staff;
	}

	/**
	 * 根据ID获得staff信息
	 *
	 * @return
	 */
	@GetMapping("/query/getUserInfoByIds")
	public List<CommonStaff> getUserInfoByIds(@RequestParam String staffIds) {
		List<CommonStaff> staffList = staffService.selectBatchIds(Arrays.asList(staffIds.split(",")));
		Map<String, CommonRole> roleMap = new HashMap<>();
		staffList.forEach(staff -> {
			if (roleMap.get(staff.getMajorRoleId()) == null) {
				roleMap.put(staff.getMajorRoleId(), roleService.selectById(staff.getMajorRoleId()));
			}
			staff.setMajorRole(roleMap.get(staff.getMajorRoleId()));
		});
		return staffList;
	}

	/**
	 * 根据公司ID获得职员信息集合
	 *
	 * @param deptId
	 * @return
	 */
	@GetMapping("/query/getStaffListByDeptId")
	public List<CommonStaff> getStaffListByDeptId(String deptId) {
		return staffService.selectList(new EntityWrapper<CommonStaff>().eq("dept_id", deptId));
	}

	/**
	 * 根据公司ID获得职员信息集合
	 *
	 * @param compId
	 * @return
	 */
	@GetMapping("/query/getStaffListByCompId")
	public List<CommonStaff> getStaffListByCompId(String compId) {
		return staffService.selectList(new EntityWrapper<CommonStaff>().eq("comp_id", compId));
	}

	/**
	 * 根据公司id获得仓储部负责人
	 * @param compId
	 * @return
	 */
	@GetMapping("/query/getPost")
	public List<CommonStaff> getPost(String compId){
		List<CommonStaffRole> staffRole = commonStaffRoleService.selectList(new EntityWrapper<CommonStaffRole>().eq("role_id","2e9d0b8540204f7abcc8e5594d44ea1a"));
		List<CommonStaff> commonStaff=commonStaffService.selectList(new EntityWrapper<CommonStaff>().eq("staff_id",staffRole.get(0).getStaffId()).eq("comp_Id",compId));
			return commonStaff;
	}



	/**
	 * 根据公司ID和库点id获得有权限的人员集合
	 *
	 * @param compId
	 * @return
	 */
	@GetMapping("/query/getStaffListByCompIdAndWarehouseId")
	public List<CommonStaff> getStaffListByCompId(String compId,String warehouseId) {
		if(compId == null || compId.isEmpty()){
			compId=AuthSecurityUtils.getStaffById(AuthSecurityUtils.getCurrentUserId()).getCompId();
		}
		if(warehouseId != null && !warehouseId.isEmpty()){
			WarehouseBaseInfo warehouseBaseInfo = warehouseBaseInfoService.selectById(warehouseId);
			String phones = warehouseBaseInfo.getOtherPersonPhone()!=null?warehouseBaseInfo.getPersonPhone()+","+warehouseBaseInfo.getOtherPersonPhone():warehouseBaseInfo.getPersonPhone();

			return staffService.selectList(new EntityWrapper<CommonStaff>().eq("comp_id", compId).in("staff_mobile_phone",phones));
		}
		else{
			return staffService.selectList(new EntityWrapper<CommonStaff>().eq("comp_id", compId));
		}
	}


	/**
	 * 通过公司ID获取部门列表
	 *
	 * @return 部门列表
	 */
	@GetMapping("/query/deptListByCompId")
	public List<CommonDepartment> getDeptListByCompId(@RequestParam(name = "compId", required = false) String compId,
			@RequestParam(required = false) String vesselBankFlag) {
		Wrapper<CommonDepartment> wrapper = new EntityWrapper<CommonDepartment>().eq("comp_id", getCompId(compId))
				.orderBy("parent_dept_id", true);
		if (!StringUtils.isEmpty(vesselBankFlag)) {
			wrapper.eq(CommonDepartment.QueryFields.VESSEL_BANK_FLAG, vesselBankFlag);
		}
		return deptInfoService.selectList(wrapper);
	}

	/**
	 * 通过公司ID获取船舶列表
	 *
	 * @return 船舶列表
	 */
	@GetMapping("/query/vesselListByCompId")
	public List<CommonVessel> getVesselListByCompId(@RequestParam(name = "compId", required = false) String compId) {
		return vesselInfoService.selectList(new EntityWrapper<CommonVessel>().eq("comp_id", getCompId(compId))
				.orderBy(CommonVessel.QueryFields.VESSEL_NAME));
	}

	/**
	 * 职员手机号重复性检查接口
	 *
	 * @return
	 * @Param 手机号
	 * @Auther ZhangMingYu
	 */
	@GetMapping("/api/checkPhone")
	public Integer checkPhone(@RequestParam String staffId, @RequestParam String phone) {
		return staffService.checkPhone(staffId, phone);
	}

	/**
	 * 职员账号重复性检查接口
	 *
	 * @Param 账号
	 * @Auther ZhangMingYu
	 */
	@GetMapping("/api/checkAccount")
	public Integer checkAccount(@RequestParam String staffId, @RequestParam String account) {
		return staffService.checkAccount(staffId, account);
	}

	/**
	 * 通过公司ID获取部门下职员
	 *
	 * @return 部门列表
	 */
	@GetMapping("/query/getDeptStaffListByCompId")
	public List<CommonStaff> deptStaffListByCompId(@RequestParam String type) {
		return staffService.deptStaffListByCompId(type, AuthSecurityUtils.getCurrentUserInfo().getCompId());
	}

	/**
	 * 根据职务查询人员
	 *
	 * @param roles 职务结合
	 * @return 返回人员关联
	 */
	@GetMapping("/query/findRole")
	public List<CommonStaff> findRoleStaff(@RequestParam String roles) {
		List<String> roleIds = new ArrayList<>();
		if (roles.contains(",")) {
			roleIds = Arrays.asList(roles.split(","));
		} else {
			roleIds.add(roles);
		}
		List<CommonStaffRole> staffRoles = staffRoleService.findStaff(roleIds);
		if (CollectionUtils.isNotEmpty(staffRoles)) {
			List<String> staffIds = staffRoles.stream().map(CommonStaffRole::getStaffId).collect(Collectors.toList());
			return staffService.findStaffs(staffIds);
		}
		return null;
	}

	/**
	 * 查询船舶的船长、大副、轮机长、大管轮
	 * 
	 * @param vesselId
	 * @return
	 */
	@GetMapping("/query/getVesselLeaders")
	public Object getVesselLeaders(@RequestParam String vesselId) {
		return staffService.getVesselLeaders(vesselId);
	}

	private String getCompId(String compId) {
		return StringUtils.isNotBlank(compId) ? compId : AuthSecurityUtils.getCurrentUserInfo().getCompId();
	}
}
