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

import com.neusoft.smart.pos.dto.member.request.CreateMemberRequest;
import com.neusoft.smart.pos.dto.member.request.GetSingleMemberRequest;
import com.neusoft.smart.pos.dto.member.request.PosRegisterMemberRequest;
import com.neusoft.smart.pos.dto.member.response.DetailedMemberResponse;
import com.neusoft.smart.pos.dto.org.request.PutShopRequest;
import com.neusoft.smart.pos.dto.org.response.PostShopResponse;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.gateway.constants.MemberErrorCode;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteMemberService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteShopService;
import com.neusoft.smart.pos.redis.bean.DeviceInfo;
import com.neusoft.smart.pos.utils.PosDeviceRedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class MemberService {
    @Autowired
    RemoteMemberService remoteMemberService;

    @Autowired
    RemoteShopService remoteShopService;

    public ResponseData<DetailedMemberResponse> findMemberByMobile(String snCode, String mobile) {
        GetSingleMemberRequest request = new GetSingleMemberRequest();
        request.setMobile(mobile);
        return findMemberByRequest(snCode, request);
    }

    public ResponseData<DetailedMemberResponse> findMemberByRequest(String snCode, GetSingleMemberRequest request) {
        DeviceInfo deviceInfo = PosDeviceRedisUtils.getDeviceInfoBySerialNo(snCode);

        if (null == deviceInfo) {
            throw new BusinessCommonException(MemberErrorCode.DEVICE_INFO_NOT_FOUND);
        }

        PostShopResponse shopResponse = remoteShopService.getShopDetail(deviceInfo.getStoreId()).getResult();

        Integer org = deviceInfo.getOrganizationId();
        Integer store = ("2").equals(shopResponse.getMembertype()) ? shopResponse.getId() : 0;

        return remoteMemberService.getMemberByCondition(request.getMobile(),  request.getCardNum(), request.getCardHardwareNum(), org, store);
    }

    public ResponseData<DetailedMemberResponse> registerMemberFromPos(String snCode, PosRegisterMemberRequest request) {
        DeviceInfo deviceInfo = PosDeviceRedisUtils.getDeviceInfoBySerialNo(snCode);

        if (null == deviceInfo) {
            throw new BusinessCommonException(MemberErrorCode.DEVICE_INFO_NOT_FOUND);
        }

        if("1".equals(request.getNeedPassword()) && StringUtils.isEmpty(request.getCardPassword())){
            throw  new BusinessCommonException(MemberErrorCode.NULL_MEMBER_CARD_PASSWORD);
        }

        CreateMemberRequest r = new CreateMemberRequest();
        r.setName(request.getName());
        r.setMobile(request.getMobile());
        r.setCardPassword(request.getCardPassword());
        r.setOrg(deviceInfo.getOrganizationId());
        r.setStore(deviceInfo.getStoreId());
        r.setBirthday(request.getBirthday());
        r.setGender(request.getGender());
        r.setAddress(request.getAddress());
        r.setAvatar(request.getAvatar());
        r.setContent(request.getContent());
        r.setCardNum(request.getCardNum());
        //r.setNeedPassword(request.getNeedPassword());

        return createMember(r);
    }

    public ResponseData<DetailedMemberResponse> createMember(CreateMemberRequest request) {
        Integer storeId = request.getStore();
        PostShopResponse shopResponse = remoteShopService.getShopDetail(storeId).getResult();

        Integer reqStore = ("2").equals(shopResponse.getMembertype()) ? storeId : 0;
        request.setStore(reqStore);
        ResponseData<DetailedMemberResponse> resp = remoteMemberService.addMember(request);

        if (!"200".equals(resp.getCode())) {
            return resp;
        }

        Integer num = remoteMemberService.getStoreMemberNum(request.getOrg(), reqStore).getResult();

        if (num == null) {
            throw new BusinessCommonException(MemberErrorCode.GET_STORE_MEMBER_NUM_ERROR);
        }

        PutShopRequest r = new PutShopRequest();
        r.setId(storeId);
        r.setMembernum(num);

        if (!"200".equals(remoteShopService.shopUpdate(r).getCode())) {
            throw new BusinessCommonException(MemberErrorCode.UPDATE_STORE_MEMBER_NUM_ERROR);
        }

        return resp;
    }
}
