package com.bs.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.hutool.core.lang.Validator;
import com.bs.system.domain.DfArea;
import com.bs.system.service.IDfAreaService;
import javax.annotation.Resource;

/**
 * 全国城市Controller
 *
 * @author bs
 * @date 2024-04-13
 */
@Api(tags = "全国城市")
@RestController
@RequestMapping("/system/area")
public class DfAreaController extends BaseController {
    @Resource
    private IDfAreaService dfAreaService;

    /**
     * 分页查询全国城市列表
     */
    @ApiOperation("分页查询全国城市列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(DfArea dfArea) {
        startPage();
        LambdaQueryWrapper<DfArea> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,dfArea);
        List<DfArea> list = dfAreaService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询全国城市列表
     */
    @ApiOperation("查询全国城市列表")
    @GetMapping("/list")
    public AjaxResult list(DfArea dfArea) {
        LambdaQueryWrapper<DfArea> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,dfArea);
        List<DfArea> list = dfAreaService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出全国城市列表
     */
    @ApiOperation("导出全国城市列表")
    @Log(title = "全国城市导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DfArea dfArea) {
        LambdaQueryWrapper<DfArea> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,dfArea);
        List<DfArea> list = dfAreaService.list(queryWrapper);
        ExcelUtil<DfArea> util = new ExcelUtil<DfArea>(DfArea. class);
        util.exportExcel(response, list, "全国城市数据");
    }

    /**
     * 获取全国城市详细信息
     */
    @ApiOperation("获取全国城市详细信息")
    @GetMapping(value = "/{areaId}")
    public AjaxResult getInfo(@PathVariable("areaId") String areaId) {
        return success(dfAreaService.getById(areaId));
    }

    /**
     * 新增全国城市
     */
    @ApiOperation("新增全国城市")
    @Log(title = "全国城市新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DfArea dfArea) {
        return toAjax(dfAreaService.save(dfArea));
    }

    /**
     * 修改全国城市
     */
    @ApiOperation("修改全国城市")
    @Log(title = "全国城市修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DfArea dfArea) {
        return toAjax(dfAreaService.updateById(dfArea));
    }

    /**
     * 删除全国城市
     */
    @ApiOperation("删除全国城市")
    @Log(title = "全国城市删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{areaIds}")
    public AjaxResult remove(@PathVariable List<Long> areaIds) {
        return toAjax(dfAreaService.removeBatchByIds(areaIds));
    }

    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<DfArea> queryWrapper,DfArea dfArea){


        //${column.columnComment}
        if(Validator.isNotEmpty(dfArea.getAreaId())){
            queryWrapper.eq(DfArea::getAreaId,dfArea.getAreaId());
        }

        //${column.columnComment}
        if(Validator.isNotEmpty(dfArea.getParentId())){
            queryWrapper.eq(DfArea::getParentId,dfArea.getParentId());
        }

        //${column.columnComment}
        if(Validator.isNotEmpty(dfArea.getName())){
            queryWrapper.eq(DfArea::getName,dfArea.getName());
        }

        //${column.columnComment}
        if(Validator.isNotEmpty(dfArea.getTenantId())){
            queryWrapper.eq(DfArea::getTenantId,dfArea.getTenantId());
        }

        //${column.columnComment}
        if(Validator.isNotEmpty(dfArea.getCreateDept())){
            queryWrapper.eq(DfArea::getCreateDept,dfArea.getCreateDept());
        }

        //${column.columnComment}
        if(Validator.isNotEmpty(dfArea.getCreateBy())){
            queryWrapper.eq(DfArea::getCreateBy,dfArea.getCreateBy());
        }

        //${column.columnComment}
        if(Validator.isNotEmpty(dfArea.getCreateTime())){
            queryWrapper.eq(DfArea::getCreateTime,dfArea.getCreateTime());
        }

        //${column.columnComment}
        if(Validator.isNotEmpty(dfArea.getUpdateBy())){
            queryWrapper.eq(DfArea::getUpdateBy,dfArea.getUpdateBy());
        }

        //${column.columnComment}
        if(Validator.isNotEmpty(dfArea.getUpdateTime())){
            queryWrapper.eq(DfArea::getUpdateTime,dfArea.getUpdateTime());
        }

        //${column.columnComment}
        if(Validator.isNotEmpty(dfArea.getDelFlag())){
            queryWrapper.eq(DfArea::getDelFlag,dfArea.getDelFlag());
        }

        }

}
