package org.jeecg.modules.service.impl;

import org.apache.commons.lang.StringUtils;
import org.jeecg.bean.UpdateTerProjectBean;
import org.jeecg.bean.UpdateTerTimeBean;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.hanHaiMember.service.impl.HanHaiMemberServiceImpl;
import org.jeecg.modules.msgDate.entity.MsgDate;
import org.jeecg.modules.msgDate.service.IMsgDateService;
import org.jeecg.modules.msgDateConfig.entity.MsgDateConfig;
import org.jeecg.modules.msgDateConfig.service.IMsgDateConfigService;
import org.jeecg.modules.msgShop.entity.MsgShop;
import org.jeecg.modules.msgShop.service.IMsgShopService;
import org.jeecg.modules.msgShopConfig.entity.MsgShopConfig;
import org.jeecg.modules.msgShopConfig.service.IMsgShopConfigService;
import org.jeecg.modules.msgTechnician.entity.MsgTechnician;
import org.jeecg.modules.msgTechnician.service.IMsgTechnicianService;
import org.jeecg.modules.msgTime.entity.MsgTime;
import org.jeecg.modules.msgTime.service.IMsgTimeService;
import org.jeecg.modules.service.TerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TerServiceImpl implements TerService {

    //获取调用权限
    @Resource
    private ShiroRealm shiroRealm;

    //获取技师数据
    @Resource
    private IMsgTechnicianService msgTechnicianService;

    //获取技师时间配置数据
    @Resource
    private IMsgDateConfigService msgDateConfigService;

    //获取技师项目配置数据
    @Resource
    private IMsgShopConfigService msgShopConfigService;

    //获取日期配置
    @Resource
    private IMsgDateService msgDateService;

    //获取时间配置
    @Resource
    private IMsgTimeService msgTimeService;

    //获取项目信息
    @Resource
    private IMsgShopService msgShopService;

    //修改用户信息
    @Resource
    private IHanHaiMemberService hanHaiMemberService;



    //初始化技师上钟时间
    @Override
    public Result<?> initTerTime(String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        //查看是否有电话号码
        if(StringUtils.isBlank(hanHaiMember.getPhone())){
            return Result.error("请先绑定手机号");
        }
        MsgTechnician technician = msgTechnicianService.lambdaQuery()
                .eq(MsgTechnician::getUserId, hanHaiMember.getId())
                .one();
        //技师不能为空
        if(technician==null){
            return Result.error("技师不存在");
        }
        //获取排期时间
        if(technician!=null){
            //获取技师排期日期
            List<MsgDate> msgDates = msgDateService.list();
            //获取技师排期时间
            List<MsgTime> msgTimes = msgTimeService.list();

            //查询是否有数据
            List<MsgDateConfig> msgDateConfigs = msgDateConfigService.lambdaQuery()
                    .eq(MsgDateConfig::getTenId, technician.getId())
                    .list();
            if(msgDateConfigs.size()>0){
                return Result.OK("已初始化排期", msgDateConfigs);
            }else{
                //先循环日期,日期内再循环时间，获得日期和时间，写入技师时间安排里
                for (int i = 0; i < msgDates.size(); i++) {
                    MsgDate msgDate = msgDates.get(i);
                    for (int j = 0; j < msgTimes.size(); j++) {
                        MsgTime msgTime = msgTimes.get(j);
                        //写入技师时间安排
                        MsgDateConfig msgDateConfig = new MsgDateConfig();
                        msgDateConfig.setDateId(msgDate.getId());
                        msgDateConfig.setTimeId(msgTime.getId());
                        msgDateConfig.setTenId(technician.getId());
                        msgDateConfig.setTimeName(msgTime.getName());
                        msgDateConfig.setPhone(hanHaiMember.getId());
                        msgDateConfig.setIsDelete("Y");
                        msgDateConfigService.save(msgDateConfig);
                    }
                }
            }
        }
        List<MsgDateConfig> msgDateConfigs = msgDateConfigService.lambdaQuery()
                .eq(MsgDateConfig::getTenId, technician.getId())
                .list();
        return Result.OK("初始化成功",msgDateConfigs);
    }



    //修改技师上钟时间
    @Override
    public Result<?> updateTerTime(String token, UpdateTerTimeBean bean) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        //传入的时间标识不能为空
        if(StringUtils.isBlank(bean.getIds())){
            return Result.error("请选择服务时间");
        }
        MsgTechnician technician = msgTechnicianService.lambdaQuery()
                .eq(MsgTechnician::getUserId, hanHaiMember.getId())
                .one();
        //技师不能为空
        if(technician==null){
            return Result.error("技师不存在");
        }
        //截取ids
        String[] idArray = bean.getIds().split(",");
        if(idArray.length>0){
            for (int i = 0; i < idArray.length; i++) {
                String id = idArray[i];
                MsgDateConfig msgDateConfig = new MsgDateConfig();
                msgDateConfig.setId(id);
                msgDateConfig.setIsDelete(bean.getIsDelete());
                msgDateConfigService.updateById(msgDateConfig);
            }
        }
        return Result.OK("修改成功");
    }




    //查询技师上钟项目
    @Override
    public Result<?> queryTerProject(String token){
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        //查看是否有电话号码
        if(StringUtils.isBlank(hanHaiMember.getPhone())){
            return Result.error("请先绑定手机号");
        }
        MsgTechnician technician = msgTechnicianService.lambdaQuery()
                .eq(MsgTechnician::getUserId, hanHaiMember.getId())
                .one();
        //技师不能为空
        if(technician==null){
            return Result.error("技师不存在");
        }
        List<MsgShopConfig> list = msgShopConfigService.lambdaQuery()
                .eq(MsgShopConfig::getTenId, technician.getId())
                .list();
        //查询不能为空
        if(list.size()==0){
            return Result.error("请先初始化项目");
        }
        List<MsgShopConfig> msgShopConfigs = new ArrayList<>();
        for (MsgShopConfig s:list) {
            MsgShopConfig msgShopConfig = s;
            msgShopConfig.setShopObject(msgShopService.getById(s.getShopId()));
            msgShopConfigs.add(msgShopConfig);
        }
        return Result.OK(msgShopConfigs);
    }


    //初始化技师上钟项目
    @Override
    public Result<?> initTerProject(String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        //查看是否有电话号码
        if(StringUtils.isBlank(hanHaiMember.getPhone())){
            return Result.error("请先绑定手机号");
        }
        //获取项目信息
        List<MsgShop> msgShops = msgShopService.list();

        MsgTechnician technician = msgTechnicianService.lambdaQuery()
                .eq(MsgTechnician::getUserId, hanHaiMember.getId())
                .one();
        if(technician!=null){
            //查询是否有项目数据
            List<MsgShopConfig> msgShopConfigs = msgShopConfigService.lambdaQuery()
                    .eq(MsgShopConfig::getTenId, technician.getId())
                    .list();
            if(msgShopConfigs.size()>0){
                return Result.OK("已初始化排期");
            }else{
                //循环写入项目信息到技师项目配置里
                for (int i = 0; i < msgShops.size(); i++) {
                    MsgShop msgShop = msgShops.get(i);
                    MsgShopConfig msgShopConfig = new MsgShopConfig();
                    msgShopConfig.setIsDelete("Y");
                    msgShopConfig.setShopId(msgShop.getId());
                    msgShopConfig.setTenId(technician.getId());
                    msgShopConfigService.save(msgShopConfig);
                }
            }
            return Result.OK("初始化成功",msgShopConfigs);
        }else{
            return Result.OK("初始化失败");
        }

    }

    //修改技师上钟项目
    @Override
    public Result<?> updateTerProject(String token, UpdateTerProjectBean bean) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        //传入的时间标识不能为空
        if(StringUtils.isBlank(bean.getIds())){
            return Result.error("请选择服务时间");
        }
        MsgTechnician technician = msgTechnicianService.lambdaQuery()
                .eq(MsgTechnician::getUserId, hanHaiMember.getId())
                .one();
        //技师不能为空
        if(technician==null){
            return Result.error("技师不存在");
        }
        //截取ids
        String[] idArray = bean.getIds().split(",");
        if(idArray.length>0){
            for (int i = 0; i < idArray.length; i++) {
                String id = idArray[i];
                MsgShopConfig msgShopConfig =  new MsgShopConfig();
                msgShopConfig.setId(id);
                msgShopConfig.setIsDelete(bean.getIsDelete());
                msgShopConfigService.updateById(msgShopConfig);
            }
        }
        return Result.OK("修改成功");
    }



    //修改用户信息
    @Override
    public  Result<?> updateUser(
            String token,
            String nickName,
            String avatarUrl,
            String phone,
            String sex
    ){
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        if (StringUtils.isNotBlank(nickName)){
            hanHaiMember.setNickName(nickName);
        }
        if (StringUtils.isNotBlank(avatarUrl)){
            hanHaiMember.setHeadImage(avatarUrl);
        }
        if (StringUtils.isNotBlank(phone)){
            hanHaiMember.setPhone(phone);
        }
        //查询手机号码是否被绑定
        HanHaiMember hanHaiMember1 = hanHaiMemberService.lambdaQuery()
                .eq(HanHaiMember::getPhone, phone)
                .one();
        if (hanHaiMember1!=null){
            if (!hanHaiMember1.getId().equals(hanHaiMember.getId())){
                return Result.error("该手机号码已被绑定");
            }
        }
        if (StringUtils.isNotBlank(sex)){
            hanHaiMember.setSex(sex);
        }
        boolean b = hanHaiMemberService.updateById(hanHaiMember);



        //查询该用户是否绑定技师
        MsgTechnician technician = msgTechnicianService.lambdaQuery()
                .eq(MsgTechnician::getUserId, hanHaiMember.getId())
                .one();
        //如果绑定了修改技师的性别
        if (technician!=null){
            technician.setSex(sex);
            msgTechnicianService.updateById(technician);
        }



        return Result.OK("修改成功");
    }


}
