package org.linlinjava.jz.db.service.hm;

import org.linlinjava.jz.db.dao.HmEmployRelationMapper;
import org.linlinjava.jz.db.domain.*;
import org.linlinjava.jz.db.service.CustomerClientService;
import org.linlinjava.jz.db.service.JzCustomerFnService;
import org.linlinjava.jz.db.util.HmResponseUtil;
import org.linlinjava.jz.db.util.hm.HmButlerTaskLogContans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fuxubing
 */
@Service
public class HmEmployRelationService {
    @Resource
    private HmEmployRelationMapper hmEmployRelationMapper;
    @Resource
    private JzCustomerFnService fnService;
    @Resource
    private CustomerClientService customerClientService;
    @Resource
    private HmAppUserService hmAppUserService;

    @Autowired
    private HmAppUserService appUserService;

    @Autowired
    HmButlerTaskChildService hmButlerTaskChildService;

    @Autowired
    private HmEmployRelationService employRelationService;


    public HmEmployRelation queryByOrderNo(String orderNo) {
        HmEmployRelationExample example = new HmEmployRelationExample();
        example.or().andOrderNoEqualTo(orderNo);
        return hmEmployRelationMapper.selectOneByExample(example);
    }

    public HmEmployRelation queryById(Long id) {
        return hmEmployRelationMapper.selectByPrimaryKey(id);
    }

    public void update(String orderNo, Integer isNeedClock, String mobile,
                       Long taskChildId) {
        HmEmployRelation hmEmployRelation = queryByOrderNo(orderNo);

        updateCustomerPhone(mobile, hmEmployRelation);

        // 手机号不同加入亲情号
//		int customerId = hmEmployRelation.getUserEmployerId().intValue();
//		CustomerClient customerClient = customerClientService.findById(customerId);
//
//		if (null != customerClient) {
//			CustomerFn fn = new CustomerFn();
//			fn.setCustomerId(customerId);
//			fn.setMobile(mobile);
//			fn.setCreateUser(adminId);
//			fn.setUpdateUser(adminId);
//			fnService.add(fn);
//		}
//
//		// 更新雇主登录手机号
//		HmAppUser hmAppUser = hmAppUserService.queryBySysUserId(String.valueOf(customerId));
//		if (null != hmAppUser) {
//			hmAppUser.setPhone(mobile);
//			hmAppUserService.update(hmAppUser);
//		}
        // 更新休息日等
        hmEmployRelation.setIsNeedClock(isNeedClock);
//		hmEmployRelation.setRestDay(restDay);
        hmEmployRelation.setUpdateTime(LocalDateTime.now());
        HmEmployRelationExample example = new HmEmployRelationExample();
        example.or().andOrderNoEqualTo(hmEmployRelation.getOrderNo());
        hmEmployRelationMapper.updateByExampleSelective(hmEmployRelation, example);

        if (null != taskChildId) {
            String operateContent = HmButlerTaskLogContans.TASK_LOG_SERVICEINFORMATION + "修改了服务信息";
            hmButlerTaskChildService.completeChildTask(taskChildId, taskChildId, operateContent);
        }
    }

    /**
     * 修改休息日
     *
     * @param restDay
     */
    public void updateRestDay(Long employRelationId, String restDay) {

        HmEmployRelation tempRelation = new HmEmployRelation();
        tempRelation.setId(employRelationId);
        tempRelation.setRestDay(restDay);

        hmEmployRelationMapper.updateByPrimaryKeySelective(tempRelation);

    }

    private void updateCustomerPhone(String mobile, HmEmployRelation hmEmployRelation) {
        HmAppUser appCustomer = hmAppUserService.queryById(hmEmployRelation.getUserEmployerId());//雇主
        HmAppUser appuser = hmAppUserService.queryById(hmEmployRelation.getUserButlerId());//管家

        // 手机号不同加入亲情号
        CustomerClient customerClient = customerClientService.findById(Integer.parseInt(appCustomer.getSysUserid()));
        if (null != customerClient) {
            CustomerFn fn = new CustomerFn();
            fn.setCustomerId(Integer.parseInt(appCustomer.getSysUserid()));
            fn.setMobile(mobile);
            fn.setCreateUser(Integer.parseInt(appuser.getSysUserid()));
            fn.setUpdateUser(Integer.parseInt(appuser.getSysUserid()));
            fnService.add(fn);
        }

        // 更新雇主登录手机号
        if (null != appCustomer && !appCustomer.getPhone().equals(mobile)) {
            HmAppUser tempUser = new HmAppUser();
            tempUser.setId(appCustomer.getId());
            tempUser.setPhone(mobile);
            hmAppUserService.update(tempUser);
        }
    }

    /**
     * 管家小程序，修改雇主登录手机号
     *
     * @param orderNo
     * @param mobile
     * @param taskChildId
     */
    public void updateCustomerTel(String orderNo, String mobile, Long taskChildId) {
        HmEmployRelation hmEmployRelation = queryByOrderNo(orderNo);

        updateCustomerPhone(mobile, hmEmployRelation);

        if (null != taskChildId) {
            String operateContent = HmButlerTaskLogContans.TASK_LOG_SERVICEINFORMATION + "修改客户登录账号为" + mobile;
            hmButlerTaskChildService.completeChildTask(taskChildId, taskChildId, operateContent);
        }
    }

    /**
     * 修改休息日和 是否需要任务打卡
     *
     * @param isNeedClock
     * @param restDay
     * @param taskChildId
     * @param orderNo
     * @return
     */
    public Object updateRestDayIsClock(Integer isNeedClock, String restDay, Long taskChildId, String orderNo) {
        HmEmployRelation hmEmployRelation = queryByOrderNo(orderNo);
        if (hmEmployRelation == null) {
            return HmResponseUtil.fail_null();
        }

        HmEmployRelation hr = new HmEmployRelation();
        hr.setIsNeedClock(isNeedClock);
        hr.setRestDay(restDay);
        hr.setUpdateTime(LocalDateTime.now());
        hr.setId(hmEmployRelation.getId());

        hmEmployRelationMapper.updateByPrimaryKeySelective(hr);
        if (null != taskChildId) {
            String operateContent = HmButlerTaskLogContans.TASK_LOG_SERVICEINFORMATION + "修改了任务信息";
            hmButlerTaskChildService.completeChildTask(taskChildId, taskChildId, operateContent);
        }
        return HmResponseUtil.ok();
    }

    /**
     * 根据三者关系获取三者关联信息
     *
     * @return
     */
    public Map<String, Object> getEmployInfo(Long employRelationId) {
        HmEmployRelation relation = employRelationService.queryById(employRelationId);
        HmAppUser userServant = appUserService.queryById(relation.getUserServantId());
        HmAppUser userCustomer = appUserService.queryById(relation.getUserEmployerId());

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("relation", relation);
        data.put("userServant", userServant);
        data.put("userCustomer", userCustomer);

        return data;
    }

    public List<HmEmployRelation> queryByUserServantId(Long userServantId) {
        HmEmployRelationExample example = new HmEmployRelationExample();
        example.or().andUserServantIdEqualTo(userServantId);
        return hmEmployRelationMapper.selectByExample(example);
    }

    public List<HmEmployRelation> queryAll() {
        HmEmployRelationExample example = new HmEmployRelationExample();
        example.or().andIsInvalidEqualTo(1);
        return hmEmployRelationMapper.selectByExample(example);
    }

    public int update(HmEmployRelation hmEmployRelation) {
        return hmEmployRelationMapper.updateByPrimaryKeySelective(hmEmployRelation);
    }
}
