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

import com.neusoft.smart.pos.dto.AlterUserRequest;
import com.neusoft.smart.pos.dto.RegisterRequest;
import com.neusoft.smart.pos.dto.org.request.PostOrgRequest;
import com.neusoft.smart.pos.dto.org.request.PostShopRequest;
import com.neusoft.smart.pos.dto.org.response.PostOrgResponse;
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.response.PostSettlementResponse;
import com.neusoft.smart.pos.dto.user.CreateUserRequest;
import com.neusoft.smart.pos.dto.user.User;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.gateway.service.OrgService;
import com.neusoft.smart.pos.gateway.service.ShopService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteOrgService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteTradeSettlementService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteUserService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static com.neusoft.smart.pos.framework.constants.ApplicationConstants.SUCCESS_CODE;
import static com.neusoft.smart.pos.framework.constants.RoleConstant.ROLE_USER;

@RestController
@RequestMapping("/unionService/register")
public class RegisterController {

    @Autowired
    RemoteOrgService remoteOrgService;
    @Autowired
    OrgService orgService;
    @Autowired
    RemoteUserService remoteUserService;
    @Autowired
    ShopService shopService;
    @Autowired
    RemoteTradeSettlementService remoteTradeSettlementService;

    @PostMapping
    public ResponseData register(@RequestBody RegisterRequest registerRequest) {
        User user = remoteUserService.getUser(registerRequest.getUsername());
        if (user != null) {
            throw new BusinessCommonException("500", "用户名已存在");
        }
        // 创建集团
        final PostOrgRequest postOrgRequest = new PostOrgRequest();
        postOrgRequest.setName(registerRequest.getName());
        postOrgRequest.setPhone(registerRequest.getPhone());
        postOrgRequest.setType("1");
        final PostOrgResponse postOrgResponse = orgService.saveOrg(postOrgRequest);
        // 创建用户
        final CreateUserRequest createUserRequest = new CreateUserRequest();
        createUserRequest.setUsername(registerRequest.getUsername());
        createUserRequest.setNickname(registerRequest.getName());
        createUserRequest.setPassword(registerRequest.getPassword());
        createUserRequest.setRole(ROLE_USER);
        createUserRequest.setOrg(postOrgResponse.getId());
        ResponseData<Integer> createUserResponse = remoteUserService.createUser(createUserRequest);
        if (!createUserResponse.getCode().equals(SUCCESS_CODE)) return createUserResponse;
        // 创建门店
        final PostShopRequest postShopRequest = new PostShopRequest();
        postShopRequest.setOrgname(registerRequest.getName());
        postShopRequest.setName(registerRequest.getName());
        postShopRequest.setAddress(registerRequest.getAddress());
        postShopRequest.setPhone(registerRequest.getPhone());
        postShopRequest.setPayeetype("2");
        postShopRequest.setOrg(postOrgResponse.getId());
        postShopRequest.setProvince(registerRequest.getProvince());
        postShopRequest.setCity(registerRequest.getCity());
        final ResponseData<PostShopResponse> postShopResponseResponseData = remoteOrgService.shopInsert(postShopRequest);
        if (!postShopResponseResponseData.getCode().equals(SUCCESS_CODE))
            return postShopResponseResponseData;
        final PostShopResponse postShopResponse = postShopResponseResponseData.getResult();
        // 创建集团结算信息
        val settlementRequest = new PostSettlementRequest();
        settlementRequest.setOrgId(postShopResponse.getOrg());
        settlementRequest.setMchName(postShopResponse.getName());
        settlementRequest.setShopId(postShopResponse.getId());
        settlementRequest.setSettlementType(postShopResponse.getPayeetype());
        ResponseData<PostSettlementResponse> postSettlementResponseResponseData = remoteTradeSettlementService.saveSettlement(settlementRequest);
        if (!postSettlementResponseResponseData.getCode().equals(SUCCESS_CODE))
            return postSettlementResponseResponseData;
        // 分配门店
        AlterUserRequest alterUserRequest = new AlterUserRequest();
        alterUserRequest.setId(createUserResponse.getResult());
        alterUserRequest.setShopIds(Collections.singletonList(postShopResponse.getId()));
        ResponseData responseData = remoteUserService.alterUser(alterUserRequest);
        if (!responseData.getCode().equals(SUCCESS_CODE)) return responseData;
        List<Integer> permissionGroups = Arrays.asList(10, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 68, 69, 70, 71, 72, 73);
        ResponseData responseData1 = remoteUserService.permissionChecked(createUserResponse.getResult(), permissionGroups);
        if (!responseData1.getCode().equals(SUCCESS_CODE)) return responseData1;
        return new ResponseData<>();
    }
}
