package com.crm.manager.controller;

import com.crm.manager.service.SysAreaService;
import com.crm.rely.backend.core.dto.base.BaseDropDownDto;
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.base.BaseSelectDto;
import com.crm.rely.backend.core.dto.system.area.SysAreaDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.system.area.SysAreaAddEntity;
import com.crm.rely.backend.core.entity.system.area.SysAreaSearchEntity;
import com.crm.rely.backend.core.entity.system.area.SysAreaUpdateEntity;
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;

import java.util.List;

@RestController
@RequestMapping("/sys/area")
public class SysAreaController {

    @Autowired
    private SysAreaService sysAreaService;

    @PostMapping("/add")
    public BaseResultDto<Boolean> add(@RequestBody @Validated SysAreaAddEntity entity) throws Exception {
        sysAreaService.save(entity);
        return BaseResultDto.success(true);
    }

    @PostMapping("/update")
    public BaseResultDto<Boolean> update(@RequestBody @Validated SysAreaUpdateEntity entity) throws Exception {
        sysAreaService.updateById(entity);
        return BaseResultDto.success(true);
    }

    @PostMapping("/search/single")
    public BaseResultDto<SysAreaDto> searchSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        SysAreaDto dto = sysAreaService.getById(entity.getId());
        return BaseResultDto.success(dto);
    }

    @PostMapping("/search/list")
    public BaseResultWithPagerDto<List<SysAreaDto>> searchList(@RequestBody SysAreaSearchEntity entity) throws Exception {
        return sysAreaService.getList(entity);
    }

    @PostMapping("/search/dd")
    public BaseResultDto<List<BaseDropDownDto>> searchDd() throws Exception {
        return BaseResultDto.success(sysAreaService.getDd());
    }

    @PostMapping("/delete")
    public BaseResultDto<Object> delete(@RequestBody SingleLongEntity entity) throws Exception {
        sysAreaService.deleteById(entity.getId());
        return BaseResultDto.success();
    }

}
