package com.bjtdba.merchant.service.impl;

import at.favre.lib.crypto.bcrypt.BCrypt;
import com.bjtdba.entity.merchant.Merchant;
import com.bjtdba.merchant.dao.MerchantDao;
import com.bjtdba.merchant.dao.StoreStaffDao;
import com.bjtdba.merchant.dao.UserDao;
import com.bjtdba.merchant.entity.StoreStaff;
import com.bjtdba.merchant.entity.User;
import com.bjtdba.merchant.service.StoreStaffService;
import com.bjtdba.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StoreStaffServiceImpl implements StoreStaffService {
    @Autowired
    private StoreStaffDao storeStaffDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private MerchantDao merchantDao;

    /**
     * 添加门店员工
     * @param storeStaff
     * @return
     */
    public Object addStoreStaff(StoreStaff storeStaff) throws UnknownHostException {
        //通过手机号查找用 user表普通用户信息
        Map<String, Object> data = new HashMap<String,Object>();
        int i=0;
        if(storeStaff==null&&storeStaff.getPhone()!=null&&!("".equals(storeStaff.getPhone()))){
            return ResponseUtil.fail();
        }
        User userInfo= new User();
        userInfo=userDao.selectByphone(storeStaff.getPhone());
        //判断用户（user）是否存在
        if(userInfo!=null){
            storeStaff.setUid(userInfo.getUid());
            storeStaffDao.insertSelective(storeStaff);
            i=storeStaff.getId();

        }else{
            User userNew=new User();
            userNew.setAccount(storeStaff.getPhone());
            userNew.setPwd(BCrypt.with(BCrypt.Version.VERSION_2Y).hashToString(10, "123456".toCharArray()));//默认密码123456
            userNew.setNickname("用户"+storeStaff.getPhone().substring(7));
            userNew.setAvatar("https://app.bjtdba.com/uploads/logo.jpg");
            userNew.setPhone(storeStaff.getPhone());
            userNew.setLast_ip(InetAddress.getLocalHost().getHostAddress());
            userNew.setIdentity(5);//推广员
            userNew.setUser_type("MP");
            int uidNew=0;
            userDao.addSave(userNew);
            uidNew=userNew.getUid();

            storeStaff.setUid(uidNew);
            storeStaffDao.insertSelective(storeStaff);
            i=storeStaff.getId();

        }

        if(i>1){
            //添加成功
            return ResponseUtil.ok();
        }else{
            //添加失败
            return ResponseUtil.fail();
        }
    }

    /**
     * 查询门店下的所员工
     * @param merId
     * @return
     */
    public Object queryStoreStaff(int merId){
        Map<String, Object> data = new HashMap<String, Object>();
        List<StoreStaff> storeStaffList=new ArrayList<StoreStaff>();
        storeStaffList= storeStaffDao.selectBymerId(merId);
        data.put("storeStaffList",storeStaffList);
        return ResponseUtil.ok(data);
    }

    public Integer  queryStoreStaffUid(int uid){
       Merchant merchant =merchantDao.selectByUId(uid);
       int num=storeStaffDao.selectCount(uid);
        if(merchant!=null&&num>0){
            return 1;
        }else{
            return 0;
        }

    }

    public Object updateStoreStaff(StoreStaff storeStaff){

        int i=0;
        if(storeStaff!=null&& storeStaff.getId()!=null&&storeStaff.getId()!=0){
            i = storeStaffDao.updateByPrimaryKeySelective(storeStaff);
        }

        if(i>0){
            //更新成功
            return ResponseUtil.ok();
        }else{
            //更新失败
            return ResponseUtil.fail();
        }
    }
}
