package com.bjtdba.merchant.controller;

import com.bjtdba.merchant.entity.StoreBranch;
import com.bjtdba.merchant.service.StoreBranchJurisdictionService;
import com.bjtdba.merchant.service.StoreBranchService;
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.*;

import java.util.Date;
import java.util.Map;


@CrossOrigin(allowedHeaders="*",methods={RequestMethod.GET,
        RequestMethod.POST, RequestMethod.DELETE, RequestMethod.OPTIONS,
        RequestMethod.HEAD, RequestMethod.PUT, RequestMethod.PATCH},origins="*")
@RestController
@RequestMapping("/merchant_service/StoreBranch")
public class StoreBranchController {
    @Autowired
    private StoreBranchService storeBranchService;
    @Autowired
    private StoreBranchJurisdictionService storeBranchJurisdictionService;

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

    /**
     * 通过门店id查询所有部门
     * @param map
     * @return
     */
    @PostMapping("/queryStoreBranchBymerid")
    public Object queryStoreBranchBymerid(@RequestBody Map<String,Object> map){
        if(!map.containsKey("merId")){
            return  ResponseUtil.fail();
        }
        int merId=Integer.parseInt(map.get("merId")+"");
        loger.info("queryStoreBranchBymerid request param merid:"+merId+"");
        if(merId==0){
            return ResponseUtil.fail();
        }
        Object ob=new Object();
        ob=storeBranchService.querySystemRole(merId);
        loger.info("queryStoreBranchBymerid return "+ob.toString());
        return ob;
    }

    /**
     * 添加用户部门
     * @param map
     * @return
     */
    @PostMapping("/addStoreBranch")
    public Object addStoreBranch(@RequestBody Map<String,Object> map){
        if(!map.containsKey("merId")){
            return  ResponseUtil.fail();
        }
        String branch =map.get("branch")+"";
        String jurisdiction=map.get("jurisdiction")+"";
        int merId=Integer.parseInt(map.get("merId")+"");
        loger.info("addStoreBranch request param  branch :"+branch.toString()+"jurisdiction:"+jurisdiction+"merId:"+merId);
        if("".equals(branch)){
            return ResponseUtil.fail();
        }
        if("".equals(jurisdiction)){
            return ResponseUtil.fail();
        }
        if(merId<=0){
            return ResponseUtil.fail();
        }
        if(!("".equals(branch))&&!("".equals(jurisdiction))&&merId>0){
            Object ob=new Object();
            StoreBranch storeBranch =new StoreBranch();
            storeBranch.setBranch(branch);
            storeBranch.setJurisdiction(jurisdiction);
            storeBranch.setMerId(merId);
            storeBranch.setCreateTime(new Date());
            ob=storeBranchService.addSystemRole(storeBranch);
            loger.info("addStoreBranch return "+ob.toString());
            return ob;

        }else{
            return ResponseUtil.fail();
        }
    }

    /**
     * 查询所有的权限
     * @return
     */
    @GetMapping("/queryJurisdiction")
    public Object queryMenu()throws Exception{
            Object ob=new Object();
            ob=storeBranchJurisdictionService.querySystemMen();
            loger.info("queryJurisdiction return "+ob.toString());
            return ob;
    }

}
