package com.bjtdba.merchant.controller;

import com.bjtdba.merchant.entity.StoreStaff;
import com.bjtdba.merchant.entity.User;
import com.bjtdba.merchant.service.StoreBranchService;
import com.bjtdba.merchant.service.StoreStaffService;
import com.bjtdba.merchant.service.UserService;
import com.bjtdba.utils.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.UnknownHostException;
import java.util.Map;

@RestController
@RequestMapping("/merchant_service/StoreStall")
public class StoreStaffController {
    @Autowired
    private StoreStaffService storeStaffService;
    @Autowired
    private StoreBranchService storeBranchService;
    @Autowired
    private UserService userService;

    private  static final Logger loger= (Logger) LoggerFactory.getLogger(StoreStaffController.class);


    /**
     * 添加商家员工
     * @param map
     * @return
     * @throws UnknownHostException
     */
    @PostMapping("/storeStafAdd")
    public Object  StoreStafAdd(@RequestBody Map<String,Object> map) throws UnknownHostException {
        if(!map.containsKey("merId")){
            return  ResponseUtil.fail();
        }
        String name=map.get("name")+"";
        String department=map.get("department")+"";
        String phone=map.get("phone")+"";
        int  merId=Integer.parseInt(map.get("merId")+"");
        loger.info("storeStafAdd request param name: "+name.toString()+"department:"+department+"phone:"+phone+"merid:"+merId);

        User user=userService.queryUserphone(phone);
        int count=storeStaffService.queryStoreStaffUid(user.getUid());
        if (count==1){
            return  ResponseUtil.fail("用户已经存在，不能添加");
        }

        if (!("".equals(name))&&!("".equals(department))&&!("".equals(phone))&&merId>0) {
            StoreStaff storeStaff =new StoreStaff();
            storeStaff.setPhone(phone);
            storeStaff.setDepartment(department);
            storeStaff.setName(name);
            storeStaff.setMerId(merId);
            Object ob=new Object();
            ob=storeStaffService.addStoreStaff(storeStaff);
            loger.info("storeStafAdd return :"+ob.toString());
            return ob;
        } else {
            return  ResponseUtil.fail();
        }

    }

    /**
     * 查询门店下的所有员工
     * @param map
     * @return
     */

    @PostMapping("/queryStoreStaff")
    public Object queryStoreStaff(@RequestBody Map<String,Object> map ){
        if(!map.containsKey("merId")){
            return  ResponseUtil.fail();
        }
        int merId=Integer.parseInt(map.get("merId")+"");
        loger.info("queryStoreStaff request param "+merId);
        if(merId!=0){

            Object ob=new Object();
            ob= storeStaffService.queryStoreStaff(merId);
            loger.info("queryStoreStaff return "+ob.toString());
            return  ob;
        }else{
            return  ResponseUtil.fail();
        }


    }

    /**
     * 更新门店员工信息
     * @param map
     * @return
     */
    @PostMapping("/updateStoreStaff")
    public  Object updateStoreStaff(@RequestBody Map<String,Object> map ){
        if(!map.containsKey("id")){
            return  ResponseUtil.fail();
        }
        String name =map.get("name")+"";
        String department =map.get("department")+"";
        String phone =map.get("phone")+"";
        int id =Integer.parseInt(map.get("id")+"");
        loger.info("updateStoreStaff request param name: "+name.toString()+"department:"+department+"phone:"+phone+"id:"+id);

        if(!("".equals(name))&&!("".equals(department))&&!("".equals(phone))&&id>0){
            Object ob=new Object();
            StoreStaff storeStaff =new StoreStaff();
            storeStaff.setPhone(phone);
            storeStaff.setDepartment(department);
            storeStaff.setName(name);
            storeStaff.setId(id);
            ob=storeStaffService.updateStoreStaff(storeStaff);
            loger.info("updateStoreStaff return "+ob.toString());
            return  ob;
        }else{
            return  ResponseUtil.fail();
        }

    }

    /**
     * 查询门店下的所有员工
     * @param map
     * @return
     */

    @PostMapping("/queryStoreBranch")
    public Object queryStoreBranch(@RequestBody Map<String,Object> map ){

        if(!map.containsKey("id")){
            return  ResponseUtil.fail();
        }
        int id=Integer.parseInt(map.get("id")+"");
        loger.info("queryStoreStaff request param "+id);
        if(id!=0){

            Object ob=new Object();
            ob= storeBranchService.queryStoreBranch(id);
            loger.info("queryStoreStaff return "+ob.toString());
            return  ob;
        }else{
            return  ResponseUtil.fail();
        }


    }


}
