package com.bjtdba.merchant.service.impl;

import com.bjtdba.merchant.dao.StoreBranchDao;
import com.bjtdba.merchant.dao.StoreBranchJurisdictionDao;
import com.bjtdba.merchant.entity.StoreBranch;
import com.bjtdba.merchant.entity.StoreBranchJurisdiction;
import com.bjtdba.merchant.service.StoreBranchService;
import com.bjtdba.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StoreBranchServiceImpl implements StoreBranchService {
    @Autowired
    private StoreBranchDao storeBranchDao;
    @Autowired
    private StoreBranchJurisdictionDao storeBranchJurisdictionDao;

    /**
     * 根据门店id查询门店下的所有部门
     * @param merid
     * @return
     */
    public Object querySystemRole(int merid){
        List<StoreBranch> systemRoleList=new ArrayList<StoreBranch>();
        StoreBranch systemRoleNew=new StoreBranch();
        Map<String,Object> map=new HashMap<String,Object>();
        systemRoleNew.setMerId(merid);
        systemRoleList=storeBranchDao.selectByExample(systemRoleNew);

        map.put("systemRoleList",systemRoleList);
        return  ResponseUtil.ok(map);

    }

    /**
     * 根据门店id查询门店下的所有部门
     * @param id
     * @return
     */
    public Object queryStoreBranch(int id){
        List<StoreBranch> systemRoleList=new ArrayList<StoreBranch>();
        StoreBranch systemRoleNew=new StoreBranch();
        List<StoreBranchJurisdiction> jurisdictionList =new ArrayList<StoreBranchJurisdiction>();
        jurisdictionList=storeBranchJurisdictionDao.selectByExample(null);
        Map<String,Object> map=new HashMap<String,Object>();
        systemRoleNew.setId(id);
        systemRoleList=storeBranchDao.selectByExample(systemRoleNew);
        String [] str=null;
        int num=0;
         if(systemRoleList!=null&&systemRoleList.size()!=0) {
             if(systemRoleList.get(0).getCreateTime()!=null&&!(systemRoleList.get(0).getCreateTime().toString().trim().equals(""))) {

                 str = systemRoleList.get(0).getJurisdiction().split(",");
             }else {
                 String objstr="";
                String [] res = systemRoleList.get(0).getJurisdiction().split(",");
                 if(res.length>0){
                     for (int i=0;i<res.length;i++){
                         for (StoreBranchJurisdiction obj: jurisdictionList) {
                             if(obj.getName().trim().equals(res[i].toString().trim())){
                                 if(num==0) {
                                     objstr += obj.getId() ;
                                 }else {
                                     objstr+=","+obj.getId();
                                 }
                                 num++;

                             }
                         }
                     }
                 }

                 str=objstr.split(",");
             }
//             map.put("jurisdiction", systemRoleList.get(0).getJurisdiction());


         }


         map.put("jurisdiction", str);
        return  ResponseUtil.ok(map);

    }

    public Object addSystemRole(StoreBranch storeBranch){
        List<StoreBranch> systemRoleList=new ArrayList<StoreBranch>();
        Map<String,Object> map=new HashMap<String,Object>();
        int i=0;
        i=storeBranchDao.insert(storeBranch);
        if (i>0){
            return ResponseUtil.ok();
        }else{
            return ResponseUtil.fail();
        }
    }


}
