package com.crm.manager.controller;

import com.crm.manager.service.ib.AgentBelongChangeFeignService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.PrefixEnum;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.BaseResultWithPagerDto;
import com.crm.rely.backend.core.dto.ib.belong.AgentBelongChangeDto;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.ib.belong.AgentBelongChangeAddEntity;
import com.crm.rely.backend.core.entity.ib.belong.AgentBelongChangeListEntity;
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.List;

/**
 * Created by max on 2020/11/4.
 */
@RestController
@RequestMapping("/agent/belong/change")
public class AgentBelongChangeController {

    @Autowired
    private AgentBelongChangeFeignService agentBelongChangeFeignService;

    @PostMapping("/add")
    public BaseResultDto<Object> add(@RequestBody AgentBelongChangeAddEntity entity, InfoEntity infoEntity) throws Exception {
        return agentBelongChangeFeignService.add(entity);
    }

    @PostMapping("/search/list")
    public BaseResultWithPagerDto<List<AgentBelongChangeDto>> searchList(@RequestBody AgentBelongChangeListEntity entity, InfoEntity infoEntity) throws Exception {
        return agentBelongChangeFeignService.searchList(entity);
    }

    @PostMapping("/batch/add")
    public BaseResultDto<List<BaseResultDto>> batchAdd(@RequestBody List<AgentBelongChangeAddEntity> entities, InfoEntity infoEntity) throws Exception {
        if (PrefixEnum.PREFIX_ADMIN.equals(infoEntity.getPrefix()) || PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
            return agentBelongChangeFeignService.batchAdd(entities);
        } else {
            return BaseResultDto.error();
        }
    }

    @PostMapping("/batch/add/v2")
    public BaseResultDto<BaseResultDto> batchAddV2(@RequestBody List<AgentBelongChangeAddEntity> entities, InfoEntity infoEntity) throws Exception {
        if (PrefixEnum.PREFIX_ADMIN.equals(infoEntity.getPrefix()) || PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
            BaseResultDto<List<BaseResultDto>> resultDto = agentBelongChangeFeignService.batchAdd(entities);
            for (BaseResultDto dto : resultDto.getData()) {
                if (Constants.SUCCESS_CODE != dto.getCode()) {
                    return BaseResultDto.success("Partially successful");
                }
            }
            return BaseResultDto.success();

        } else {
            return BaseResultDto.error();
        }
    }

}
