package com.ssm.manage.service.impl;

import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.SsmBaseStaffDao;
import com.ssm.manage.dao.SsmBaseWorkTypeDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.query.BaseWorkTypeRequest;
import com.ssm.manage.query.StoreRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.service.SsmBaseStaffService;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.service.SsmBaseWorkTypeService;
import com.ssm.util.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;

@Service
@Slf4j
public class SsmBaseWorkTypeServiceImpl  extends BaseServiceImpl<BaseWorkType> implements SsmBaseWorkTypeService {

    @Autowired
    private SsmBaseWorkTypeDao dao;

    @Autowired
    private SsmBaseStaffService ssmBaseStaffService;
    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @Override
    public SsmBaseWorkTypeDao getDao() {
        return dao;
    }

    private static String staffKey = "staffKey";

    @Autowired
    private RedisClient<StaffResponse> redisClient;

    @Override
    public List<BaseWorkType> selectPage(BaseWorkTypeRequest baseWorkTypeRequest) throws SmsException, ParseException {
        StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(baseWorkTypeRequest.getStoreId());
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
        BaseStores baseStores1 = pagesorganizations.stream().filter(baseStores -> null == baseStores.getParentId()).findAny().orElse(null);

        BaseWorkType baseWorkType = new BaseWorkType();
        baseWorkType.setStoreId(baseStores1.getId());
        List<BaseWorkType> select = dao.select(baseWorkType);
        return select;
    }

    @Override
    public int add(BaseWorkTypeRequest baseWorkTypeRequest, String phone) throws SmsException {
        BaseWorkType baseWorkType = new BaseWorkType();
        BeanUtils.copyProperties(baseWorkTypeRequest,baseWorkType);
        if(null == baseWorkType.getStoreId()){
            throw new SmsException(ResponseCode.MSG,"门店不存在，请确认登录是否有效");
        }
        if(null == baseWorkType.getWordTypeNum()){
            throw new SmsException(ResponseCode.MSG,"编码不能为空");
        }
        if(null == baseWorkType.getWordTypeName()){
            throw new SmsException(ResponseCode.MSG,"名称不能为空");
        }
        if(!this.CheckRepeatNum(baseWorkType.getStoreId(),baseWorkType.getWordTypeNum())){
            throw new SmsException(ResponseCode.MSG,"该门店已经存在该工种编码");
        }
        if(!this.CheckRepeatName(baseWorkType.getStoreId(),baseWorkType.getWordTypeName())){
            throw new SmsException(ResponseCode.MSG,"该门店已经存在该工种名称");
        }
        StaffResponse staffResponse = new StaffResponse();
        Set<String> keys = redisClient.keys(getStaffKey(phone) + "*");
        //从redis查询 设置录单人信息 app使用
        if (keys.size() > 0){
            String key =  new ArrayList<>(keys).get(0);
            staffResponse = redisClient.get(key);
        }
        baseWorkType.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        baseWorkType.setInputId(staffResponse.getId());
        baseWorkType.setInputBy(staffResponse.getStaffName());
        baseWorkType.setInputDate(new Date());
        baseWorkType.setState(1);
        return dao.insert(baseWorkType);
    }

    @Override
    public int update(BaseWorkTypeRequest baseWorkTypeRequest, String phone) throws SmsException {
        BaseWorkType baseWorkType = new BaseWorkType();
        BeanUtils.copyProperties(baseWorkTypeRequest,baseWorkType);
        if(null == baseWorkType.getWordTypeNum()){
            throw new SmsException(ResponseCode.MSG,"编码不能为空");
        }
        if(null == baseWorkType.getWordTypeName()){
            throw new SmsException(ResponseCode.MSG,"名称不能为空");
        }
        if(!this.CheckRepeatNum(baseWorkType.getStoreId(),baseWorkType.getWordTypeNum(),baseWorkType.getId())){
            throw new SmsException(ResponseCode.MSG,"该门店已经存在该工种编码");
        }
        if(!this.CheckRepeatName(baseWorkType.getStoreId(),baseWorkType.getWordTypeName(),baseWorkType.getId())){
            throw new SmsException(ResponseCode.MSG,"该门店已经存在该工种名称");
        }

        StaffResponse staffResponse = new StaffResponse();
        Set<String> keys = redisClient.keys(getStaffKey(phone) + "*");
        //从redis查询 设置录单人信息 app使用
        if (keys.size() > 0){
            String key =  new ArrayList<>(keys).get(0);
            staffResponse = redisClient.get(key);
        }
        baseWorkType.setUpdateId(staffResponse.getId());
        baseWorkType.setUpdateBy(staffResponse.getStaffName());
        baseWorkType.setUpdateDate(new Date());
        return dao.updateByPrimaryKeySelective(baseWorkType);
    }

    @Override
    public int updateState(BaseWorkTypeRequest baseWorkTypeRequest, String phone) throws SmsException {
        BaseWorkType baseWorkType = new BaseWorkType();
        BeanUtils.copyProperties(baseWorkTypeRequest,baseWorkType);
        if(0==baseWorkType.getState()){
            BaseStaff baseStaff = new BaseStaff();
            baseStaff.setWordTypeId(baseWorkTypeRequest.getId());
            List<BaseStaff> select = ssmBaseStaffService.select(baseStaff);
            if(select.size()>0){
                throw new SmsException(ResponseCode.MSG,"当前工种类型存在员工关联，不能停用");
            }
        }
        return dao.updateByPrimaryKeySelective(baseWorkType);
    }

    @Override
    public int delete(BaseWorkTypeRequest baseWorkTypeRequest) throws SmsException {
        if(null == baseWorkTypeRequest.getId()){
            throw new SmsException(ResponseCode.MSG,"删除工种的依据Id不存在");
        }
        //校验是否正在使用
        BaseStaff baseStaff = new BaseStaff();
        baseStaff.setWordTypeId(baseWorkTypeRequest.getId());
        List<BaseStaff> select = ssmBaseStaffService.select(baseStaff);
        if(select.size()>0){
            throw new SmsException(ResponseCode.MSG,"当前工种类型存在员工关联，不能删除");
        }else{
            return dao.deleteByPrimaryKey(baseWorkTypeRequest.getId());
        }
    }

    /**
     * 新增校验同门店编码和，名称是否重复
     * @param wordTypeNum
     * @return
     */
    public Boolean CheckRepeatNum(Long storeId,String wordTypeNum){
        BaseWorkType baseWorkType = new BaseWorkType();
        baseWorkType.setStoreId(storeId);
        baseWorkType.setWordTypeNum(wordTypeNum);
        BaseWorkType baseWorkType1 = dao.selectOne(baseWorkType);
        if(null == baseWorkType1){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 新增校验同门店编码和，名称是否重复
     * @param wordTypeName
     * @return
     */
    public Boolean CheckRepeatName(Long storeId,String wordTypeName){
        BaseWorkType baseWorkType = new BaseWorkType();
        baseWorkType.setStoreId(storeId);
        baseWorkType.setWordTypeName(wordTypeName);
        BaseWorkType baseWorkType1 = dao.selectOne(baseWorkType);
        if(null == baseWorkType1){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 修改校验同门店编码和，名称是否重复
     * @param wordTypeNum
     * @return
     */
    public Boolean CheckRepeatNum(Long storeId,String wordTypeNum,Long id){
        BaseWorkType baseWorkType = new BaseWorkType();
        baseWorkType.setStoreId(storeId);
        baseWorkType.setWordTypeNum(wordTypeNum);
        BaseWorkType baseWorkType1 = dao.selectOne(baseWorkType);
        if(null != baseWorkType1){
            if(baseWorkType1.getId().equals(id)){
                return true;
            }else{
                return false;
            }
        }else{
            return true;
        }
    }

    /**
     * 修改校验同门店编码和，名称是否重复
     * @param wordTypeName
     * @return
     */
    public Boolean CheckRepeatName(Long storeId,String wordTypeName,Long id){
        BaseWorkType baseWorkType = new BaseWorkType();
        baseWorkType.setStoreId(storeId);
        baseWorkType.setWordTypeName(wordTypeName);
        BaseWorkType baseWorkType1 = dao.selectOne(baseWorkType);
        if(null != baseWorkType1){
            if(baseWorkType1.getId().equals(id)){
                return true;
            }else{
                return false;
            }
        }else{
            return true;
        }
    }

    public String getStaffKey(String phone){
        return staffKey + phone;
    }
}
