package com.gic.haoban.manage.web.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.gic.clerk.api.dto.ClerkDTO;
import com.gic.clerk.api.service.ClerkService;
import com.gic.commons.util.EntityUtil;
import com.gic.haoban.common.utils.HaobanResponse;
import com.gic.haoban.manage.api.dto.DepartmentDTO;
import com.gic.haoban.manage.api.dto.StaffDTO;
import com.gic.haoban.manage.api.dto.StaffDepartmentRelatedDTO;
import com.gic.haoban.manage.api.enums.NoticeMessageTypeEnum;
import com.gic.haoban.manage.api.service.DepartmentApiService;
import com.gic.haoban.manage.api.service.StaffApiService;
import com.gic.haoban.manage.api.service.StaffDepartmentRelatedApiService;
import com.gic.haoban.manage.api.util.notify.NoticeMessageUtil;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import com.gic.haoban.manage.web.vo.ClerkVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;
import java.util.stream.Collectors;

@Deprecated
public class ClerkController extends WebBaseController{
	@Autowired
	private ClerkService clerkService;
	@Autowired
	private DepartmentApiService departmentApiService;
	@Autowired
	private StaffDepartmentRelatedApiService staffDepartmentRelatedApiService;
	@Autowired
	private StaffApiService staffApiService;
	
	//未绑定的clerkCode列表
	@RequestMapping("/clerk-code-list")
	public HaobanResponse clerkCodeList(String departmentId) {
		List<StaffDepartmentRelatedDTO> list = staffDepartmentRelatedApiService.listByDepartmentId(departmentId);
		List<ClerkVo> result  = new ArrayList<ClerkVo>();
		if(StringUtils.isAnyBlank(departmentId)){
			return resultResponse(HaoBanErrCode.ERR_2);
		}
		DepartmentDTO departmentDTO = departmentApiService.selectById(departmentId);
		if(departmentDTO == null){
			return resultResponse(HaoBanErrCode.ERR_10009);
		}
		String storeId = departmentDTO.getRelatedId();
		if(StringUtils.isAnyBlank(departmentId)){
			return resultResponse(HaoBanErrCode.ERR_10009);
		}
		String bindFlag = "0";
		//绑定的clerk
		if("1".equals(bindFlag)){
			// 调陶接口获取详情数据
			if(CollectionUtil.isNotEmpty(list)){
				List<String> clerkCodes = list.stream().filter(s->s.getClerkCode()!=null).map(s->s.getClerkCode()).collect(Collectors.toList());
				if(CollectionUtil.isNotEmpty(clerkCodes)){
					List<ClerkDTO> clerkList = clerkService.listClerk(storeId, clerkCodes, true);
					result = EntityUtil.changeEntityListByJSON(ClerkVo.class, clerkList);
				}
			}
		}else{
			//未绑定的clerk
			// 调陶接口获取详情数据
			if(CollectionUtil.isNotEmpty(list)){
				List<String> clerkCodes = list.stream().filter(s->s.getClerkCode()!=null).map(s->s.getClerkCode()).collect(Collectors.toList());
				if(CollectionUtil.isNotEmpty(clerkCodes)){
					List<ClerkDTO> clerkList = clerkService.listClerk(storeId, clerkCodes, false);
					result = EntityUtil.changeEntityListByJSON(ClerkVo.class, clerkList);
				}else{
					clerkCodes.add("xxxXXXX");
					List<ClerkDTO> clerkList = clerkService.listClerk(storeId, clerkCodes, false);
					result = EntityUtil.changeEntityListByJSON(ClerkVo.class, clerkList);
				}
			}else{
				List<String> clerkCodes = new ArrayList();
				clerkCodes.add("xxxXXXX");
				List<ClerkDTO> clerkList = clerkService.listClerk(storeId, clerkCodes, false);
				result = EntityUtil.changeEntityListByJSON(ClerkVo.class, clerkList);
			}
		}
		return resultResponse(HaoBanErrCode.ERR_1,result);
		
	}
	//成员绑定
	@RequestMapping("/staff-bind")
	public HaobanResponse staffBind(String toClerkCode,String fromStaffDepartmentRelatedId) {
		if(StringUtils.isAnyBlank(toClerkCode,fromStaffDepartmentRelatedId)){
			return resultResponse(HaoBanErrCode.ERR_2);
		}
		StaffDepartmentRelatedDTO staffDepartmentRelatedDTO = staffDepartmentRelatedApiService.getByStaffDepartmentRelatedId(fromStaffDepartmentRelatedId);
		String oldClerkCode = staffDepartmentRelatedDTO.getClerkCode();
		//将指定人绑定clerkCode
		StaffDepartmentRelatedDTO dto = new StaffDepartmentRelatedDTO();
		dto.setStaffDepartmentRelatedId(fromStaffDepartmentRelatedId);
		dto.setClerkCode(toClerkCode);
		dto.setUpdateTime(new Date());
		staffDepartmentRelatedApiService.updateById(dto);
		//更新gic数据
		DepartmentDTO departmentDTO = departmentApiService.selectById(staffDepartmentRelatedDTO.getDepartmentId());
		String storeId = departmentDTO.getRelatedId();
		List<String> clerkCodes = new ArrayList<>();
		clerkCodes.add(toClerkCode);
		List<ClerkDTO> list = clerkService.listClerk(storeId, clerkCodes, true);
		StaffDTO staffDTO = staffApiService.selectById(staffDepartmentRelatedDTO.getStaffId());
		for(ClerkDTO clerkDTO :list){
			clerkDTO.setPhoneNumber(staffDTO.getPhoneNumber());
			clerkDTO.setClerkName(staffDTO.getStaffName());
			clerkDTO.setHeadImgUrl(StringUtils.isEmpty(staffDTO.getHeadImg())?"":staffDTO.getHeadImg());
			clerkService.updateClerk(clerkDTO);
		}
		//清空gic原来的手机号
		if(StringUtils.isNotBlank(staffDepartmentRelatedDTO.getClerkCode())){
			List<String> oldClerkCodes = new ArrayList<>();
			oldClerkCodes.add(staffDepartmentRelatedDTO.getClerkCode());
			List<ClerkDTO> oldList = clerkService.listClerk(storeId, oldClerkCodes, true);
			for(ClerkDTO clerkDTO :oldList){
				clerkDTO.setPhoneNumber("--");
				clerkDTO.setClerkName(clerkDTO.getClerkName()+"(已解绑)");
				clerkDTO.setHeadImgUrl("");
				clerkService.updateClerk(clerkDTO);
			}
		}
		
		//发送消息
		if(StringUtils.isNotBlank(oldClerkCode)){
			//发送消息
			ClerkDTO leader = clerkService.getClerkLeaderByStoreId(storeId);
			if (leader == null) {
				leader = new ClerkDTO();
			}
			String clerkId = leader.getClerkId();
			String storeName = departmentDTO.getDepartmentName();
			String staffName = staffDTO.getStaffName();
			String leaderName = leader.getClerkName();
            Map<String, String> params = new HashMap<String, String>();
            params.put("leaderName", leaderName);
            params.put("storeName", storeName);
            params.put("staffName", staffName);
            params.put("oldClerkCode", oldClerkCode);

			JSONObject jsonObject = new JSONObject();
			jsonObject.put("storeId", storeId);

            NoticeMessageUtil.sendNoticeMessage(leader.getEnterpriseId(), clerkId, NoticeMessageTypeEnum.CUSTOMER_UNBIND.getType(), null, params, jsonObject);
        }
        return resultResponse(HaoBanErrCode.ERR_1);
	}

}
