package com.crm.manager.controller;

import com.crm.manager.service.SysAgentLoginTypeService;
import com.crm.manager.service.SysLoginTypeService;
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.system.logintype.SysLoginTypeListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.system.logintype.SysLoginTypeAddEntity;
import com.crm.rely.backend.core.entity.system.logintype.SysLoginTypeDeleteEntitys;
import com.crm.rely.backend.core.entity.system.logintype.SysLoginTypeSearchEntity;
import com.crm.rely.backend.core.entity.system.logintype.SysLoginTypeUpdateEntity;
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;

/**
 * 外佣暗点 加点管理
 */
@RestController
@RequestMapping("/login/type")
public class SysLoginTypeController {

    @Autowired
    private SysLoginTypeService sysLoginTypeService;

    @Autowired
    private SysAgentLoginTypeService sysAgentLoginTypeService;

    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated SysLoginTypeAddEntity entity) throws Exception {
        sysLoginTypeService.add(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated SysLoginTypeDeleteEntitys entitys) throws Exception {

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

    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated SysLoginTypeUpdateEntity entity) throws Exception {
        sysLoginTypeService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/search/list")
    public ResultWithPagerDto<SysLoginTypeListDto> pageList(@RequestBody SysLoginTypeSearchEntity entity) throws Exception {

        return sysLoginTypeService.searchPageList(entity);
    }

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

    @PostMapping("/manager/search/list")
    public ResultWithPagerDto managerPageList(@RequestBody SysLoginTypeSearchEntity entity,
                                                                   InfoEntity infoEntity) throws Exception {
        if (infoEntity.getIbInfo() != null) {
            entity.setAgentId(infoEntity.getIbInfo().getId());
            if (sysAgentLoginTypeService.existByAgentId(infoEntity.getIbInfo().getId())) {
                return sysAgentLoginTypeService.searchPageList(entity);
            }
        }
        return sysLoginTypeService.searchPageList(entity);
    }

}
