package com.neusoft.smart.pos.org.service;

import com.neusoft.smart.pos.dto.org.request.OrgQueryRequest;
import com.neusoft.smart.pos.dto.org.request.PostOrgRequest;
import com.neusoft.smart.pos.dto.org.request.PutOrgRequest;
import com.neusoft.smart.pos.dto.org.response.PostOrgResponse;
import com.neusoft.smart.pos.framework.constants.CommonErrorCode;
import com.neusoft.smart.pos.framework.constants.RoleConstant;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.framework.utils.Utility;
import com.neusoft.smart.pos.org.constants.OrgShopErrorCode;
import com.neusoft.smart.pos.org.dto.User;
import com.neusoft.smart.pos.org.persistent.domain.Org;
import com.neusoft.smart.pos.org.repository.OrgRepository;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrgService {
    @Resource
    RemoteUserService remoteUserService;

    @Resource
    public OrgRepository orgRepository;

    public PostOrgResponse selectOrgByUsername(String username) {
        User user = remoteUserService.getUser(username);
        if (user != null) {
            Integer orgId = user.getOrg();
            PostOrgResponse resResult = new PostOrgResponse();
            BeanUtils.copyProperties(orgRepository.selectByPrimaryKey(orgId),resResult);
            return resResult;
        }
        return null;
    }
    
    public PostOrgResponse insertOrg(PostOrgRequest orgRequest){
        //1.根据集团名称校验集团信息是否已经存在
        if(StringUtils.isEmpty(orgRequest.getName())){
            throw new BusinessCommonException(OrgShopErrorCode.ORG_NAME_IS_NULL);
        }
        Org org = orgRepository.queryOrgByName(orgRequest.getName());
        if(null != org){
            throw new BusinessCommonException(OrgShopErrorCode.ORG_NAME_REPEAT);
        }
        //2.添加集团信息
        org = new Org();
        BeanUtils.copyProperties(orgRequest,org);
        orgRepository.insertSelective(org);
        PostOrgResponse orgResponse = new PostOrgResponse();
        final PostOrgResponse postOrgResponse = selectOrgById(org.getId());
        if(postOrgResponse!=null) {BeanUtils.copyProperties(postOrgResponse,orgResponse);}
        else{
            BeanUtils.copyProperties(org,orgResponse);
        }
    	return orgResponse;
    }
    
    public int updateOrg(PutOrgRequest orgReq){
    	if (!StringUtils.isEmpty(orgReq.getName())){
	    	Org org = orgRepository.queryOrgByName(orgReq.getName());
	        if(null != org && !orgReq.getId().equals(org.getId())){
	            throw new BusinessCommonException(OrgShopErrorCode.ORG_NAME_REPEAT);
	        }
    	}
    	Org org = new Org();
    	BeanUtils.copyProperties(orgReq,org);
    	return orgRepository.updateByPrimaryKeySelective(org);
    }
    
    public int deleteOrg(int orgId){
    	int org_Id =RequestHeaderUtil.getOrgIdByRole(orgId);
    	Org org = new Org();
    	org.setStatus("0");
    	org.setId(org_Id);
    	return orgRepository.updateByPrimaryKeySelective(org);
    }
    
    public List<PostOrgResponse> selectOrgsByCondi(OrgQueryRequest orgQueryRequest){
    	Integer orgId = RequestHeaderUtil.getOrgId();
		if (null != orgId && (RequestHeaderUtil.getRole().equals(RoleConstant.ROLE_ADMIN) || RequestHeaderUtil.getRole().equals(RoleConstant.ROLE_USER))){
			orgQueryRequest.setId(Integer.toString(orgId));
		}else if (null == orgId && !RequestHeaderUtil.getRole().equals(RoleConstant.ROLE_SA)){
			throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
		}
		
    	if (!StringUtils.isEmpty(orgQueryRequest.getId()) && !Utility.isNumeric(orgQueryRequest.getId())) throw new BusinessCommonException(OrgShopErrorCode.ID_IS_NOT_NUMBER);
    	List<Org> orgs = orgRepository.selectByExample(orgQueryRequest);
        List<PostOrgResponse> result = new ArrayList<>();
        orgs.forEach(po -> {
        	PostOrgResponse response = new PostOrgResponse();
			BeanUtils.copyProperties(po, response);
			result.add(response);
		});
        return result;
    }
    
    public int countOrgsByCondi(OrgQueryRequest orgQueryRequest){
    	if (!StringUtils.isEmpty(orgQueryRequest.getId()) && !Utility.isNumeric(orgQueryRequest.getId())) throw new BusinessCommonException(OrgShopErrorCode.ID_IS_NOT_NUMBER);
        return orgRepository.countOrgsByCondi(orgQueryRequest);
    }
    
    public PostOrgResponse selectOrgById(int orgId){
    	Integer org_Id =RequestHeaderUtil.getOrgIdByRole(orgId);
    	if(org_Id==null) return null;
    	PostOrgResponse resResult = new PostOrgResponse();
        BeanUtils.copyProperties(orgRepository.selectByPrimaryKey(org_Id),resResult);
        return resResult;
    }
}
