package com.crm.manager.controller;

import com.crm.manager.service.SysCountryService;
import com.crm.manager.service.custom.SysCountryFeignService;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.country.SysCountryDto;
import com.crm.rely.backend.core.dto.country.SysCountrySearchListDto;
import com.crm.rely.backend.core.dto.risk.group.leverage.RiskGroupLeverageDto;
import com.crm.rely.backend.core.dto.risk.symbol.RiskSymbolDto;
import com.crm.rely.backend.core.dto.system.bank.SysBankSearchListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.country.*;
import com.crm.rely.backend.core.entity.risk.group.leverage.RiskGroupLeverageSearchEntity;
import com.crm.rely.backend.core.entity.system.bank.SysBankAddEntity;
import com.crm.rely.backend.core.entity.system.bank.SysBankDeleteEntitys;
import com.crm.rely.backend.core.entity.system.bank.SysBankUpdateEntity;
import com.crm.rely.backend.util.StringUtil;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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;

/**
 * @author: houn
 */
@RestController
@RequestMapping("/country")
public class SysCountryController {

    @Autowired
    private SysCountryFeignService sysCountryFeignService;

    @Autowired
    private SysCountryService sysCountryService;

    /**
     * 根据pid获取列表
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/get")
    public BaseResultDto getAllCountry(@RequestBody SysCountryEntity entity) throws Exception {

        if (entity.getPid() == null) {
            entity.setPid(0L);
        }
        BaseResultDto resultDto = sysCountryFeignService.getAllCountry(entity);

        return resultDto;
    }

    @PostMapping("/get/state")
    public BaseResultDto getManagerState(@RequestBody SysCountryStateEntity entity) throws Exception {

        return sysCountryFeignService.getStates(entity);
    }

    /**
     * 添加城市
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated SysCountryAddEntity entity) throws Exception {

        if (entity.getPid() == null) {
            entity.setPid(0l);
            if(Strings.isNullOrEmpty(entity.getCode()) || Strings.isNullOrEmpty(entity.getCallingCode())) {
                return BaseResultDto.error("编码或区号为空");
            }
        }
        if (entity.getValidForStarted() == null) {
            entity.setValidForStarted(1);
        }
        sysCountryService.add(entity);
        return BaseResultDto.success();
    }

    /**
     * 删除城市
     *
     * @param entitys
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated SysCountryDeleteEntitys entitys) throws Exception {

        sysCountryService.delete(entitys);
        return BaseResultDto.success();
    }

    /**
     * 修改城市
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated SysCountryUpdateEntity entity) throws Exception {
        if (entity.getPid() == null) {
            entity.setPid(0l);
        }
        if (entity.getValidForStarted() == null) {
            entity.setValidForStarted(1);
        }
        sysCountryService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/search/list")
    public ResultWithPagerDto<SysCountrySearchListDto> pageList(@RequestBody SysCountrySearchListEntity entity) throws Exception {
         return sysCountryService.searchPageList(entity);
    }

    @PostMapping("/search/single")
    public BaseResultDto searchSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        SysCountrySearchListDto result = sysCountryService.searchSingle(entity);
        return BaseResultDto.success(result);
    }

}
