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

import com.neusoft.smart.pos.dto.inventory.request.InventoryConfigRequest;
import com.neusoft.smart.pos.dto.inventory.response.InventoryGoodsConfigResponse;
import com.neusoft.smart.pos.dto.member.request.CreateMemberLevelRequest;
import com.neusoft.smart.pos.dto.org.request.PostShopRequest;
import com.neusoft.smart.pos.dto.org.request.PutShopRequest;
import com.neusoft.smart.pos.dto.org.response.PostShopResponse;
import com.neusoft.smart.pos.dto.trade.request.PostSettlementRequest;
import com.neusoft.smart.pos.dto.trade.request.PutSettlementRequest;
import com.neusoft.smart.pos.framework.constants.ApplicationConstants;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteInventoryService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteMemberService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteShopService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteTradeSettlementService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;

@Service
public class ShopService {
    
    @Autowired
    private RemoteShopService remoteShopService;
    
    @Autowired
    private RemoteTradeSettlementService remoteTradeSettlementService;
    
    @Autowired
    private RemoteMemberService remoteMemberService;
    
    @Autowired
    private RemoteInventoryService remoteInventoryService;
    
    /**
     * 门店信息保存接口
     *
     * @param shopRequest
     * @return
     */
    public PostShopResponse saveShop(PostShopRequest shopRequest) {
        //1.调用集团门店管理服务，新建集团信息
        ResponseData<PostShopResponse> responseData = remoteShopService.saveShop(shopRequest);
        val shopResponse = responseData.getResult();
        
        //2.添加集团结算信息，默认开通现金支付
        if (ApplicationConstants.SUCCESS_CODE.equals(responseData.getCode()) && null != shopResponse) {
            val settlementRequest = new PostSettlementRequest();
            settlementRequest.setOrgId(shopResponse.getOrg());
            settlementRequest.setMchName(shopResponse.getName());
            settlementRequest.setShopId(shopResponse.getId());
            settlementRequest.setSettlementType(shopRequest.getPayeetype());
            remoteTradeSettlementService.saveSettlement(settlementRequest);
        } else {
            throw new BusinessCommonException(responseData);
        }
        
        //3.创建默认会员卡等级
        CreateMemberLevelRequest createMemberLevelRequest = new CreateMemberLevelRequest();
        createMemberLevelRequest.setName("初级");
        createMemberLevelRequest.setLevel(1);
        createMemberLevelRequest.setRequiredRechargeAmount(new BigDecimal(0));
        createMemberLevelRequest.setDiscount(new BigDecimal(10));
        createMemberLevelRequest.setOrg(shopResponse.getOrg());
        createMemberLevelRequest.setStore(("2").equals(shopResponse.getMembertype()) ? shopResponse.getId() : 0);
        remoteMemberService.createMemberLevel(createMemberLevelRequest);
        //4.创建进销存配置
        InventoryConfigRequest InventoryConfigRequest = new InventoryConfigRequest();
        InventoryConfigRequest.setOrgId(shopResponse.getOrg());
        InventoryConfigRequest.setShopId(shopResponse.getId());
        InventoryConfigRequest.setCanNegativeStorage(shopRequest.getCanNegativeStorage());
        InventoryConfigRequest.setIsLock(shopRequest.getIsLock());
        InventoryConfigRequest.setNeedApproval(shopRequest.getNeedApproval());
        
        remoteInventoryService.createConfig(InventoryConfigRequest);
        return responseData.getResult();
    }
    
    /**
     * 门店信息更新接口
     *
     * @param shopRequest
     * @return
     */
    public Integer updateShop(PutShopRequest shopRequest) {
        //1.调用集团门店管理服务，新建集团信息
        ResponseData<Integer> responseData = remoteShopService.shopUpdate(shopRequest);
        
        //2.添加集团结算信息，默认开通现金支付
        if (ApplicationConstants.SUCCESS_CODE.equals(responseData.getCode()) && responseData.getResult() > 0) {
            if (!StringUtils.isEmpty(shopRequest.getPayeetype())) {
                val settlementRequest = new PutSettlementRequest();
                settlementRequest.setOrgId(shopRequest.getOrg());
                settlementRequest.setShopId(shopRequest.getId());
                settlementRequest.setSettlementType(shopRequest.getPayeetype());
                remoteTradeSettlementService.updateSettlement(settlementRequest);
            }
        } else {
            throw new BusinessCommonException(responseData);
        }
        //3.修改进销存配置
        InventoryConfigRequest InventoryConfigRequest = new InventoryConfigRequest();
        InventoryConfigRequest.setId(shopRequest.getInventoryConfigId());
        InventoryConfigRequest.setCanNegativeStorage(shopRequest.getCanNegativeStorage());
        InventoryConfigRequest.setIsLock(shopRequest.getIsLock());
        InventoryConfigRequest.setNeedApproval(shopRequest.getNeedApproval());
        InventoryConfigRequest.setShopId(shopRequest.getId());
        InventoryConfigRequest.setOrgId(shopRequest.getOrg());
        if (null == InventoryConfigRequest.getId()) {//兼容以前没创建过数据的门店
            remoteInventoryService.createConfig(InventoryConfigRequest);
        } else {
            remoteInventoryService.updateConfig(InventoryConfigRequest);
        }
        
        
        return responseData.getResult();
    }
    
    /**
     * @param shopId
     * @return
     */
    public PostShopResponse get(Integer shopId) {
        ResponseData<PostShopResponse> postShopResponse = remoteShopService.getShopDetail(shopId);
        if (!ApplicationConstants.SUCCESS_CODE.equals(postShopResponse.getCode())) {
            throw new BusinessCommonException(postShopResponse);
        }
        PostShopResponse result = postShopResponse.getResult();
        ResponseData<InventoryGoodsConfigResponse> goodsConfigResponse = remoteInventoryService.getConfig(result.getOrg(), result.getId());
        if (ApplicationConstants.SUCCESS_CODE.equals(goodsConfigResponse.getCode()) && null != goodsConfigResponse.getResult()) {
            result.setInventoryConfigId(goodsConfigResponse.getResult().getId());
            result.setIsLock(goodsConfigResponse.getResult().getIsLock());
            result.setCanNegativeStorage(goodsConfigResponse.getResult().getCanNegativeStorage());
            result.setNeedApproval(goodsConfigResponse.getResult().getNeedApproval());
        }
        return result;
    }
}
