package com.xjrsoft.module.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.module.organization.service.IUserService;
import com.xjrsoft.module.system.dto.AddAreaDto;
import com.xjrsoft.module.system.dto.UpdateAreaDto;
import com.xjrsoft.module.system.entity.Area;
import com.xjrsoft.module.system.service.IAreaService;
import com.xjrsoft.module.system.vo.AreaListVo;
import com.xjrsoft.module.system.vo.AreaVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 行政区域表 前端控制器
 * </p>
 *
 * @author tzx
 * @since 2022-06-16
 */
@RestController
@RequestMapping(GlobalConstant.SYSTEM_MODULE_PREFIX + "/area")
@Api(value = GlobalConstant.SYSTEM_MODULE_PREFIX + "/area", tags = "行政区域")
@AllArgsConstructor
public class AreaController {

    private final IAreaService areaService;

    private final IUserService userService;

    @GetMapping("/province")
    @ApiOperation(value = "获取所有省份")
    public R getAllProvinceArea(@RequestParam(required = false) String keyword) {
        List<Area> provinceList = areaService.list(Wrappers.<Area>query().lambda().eq(Area::getParentId, GlobalConstant.FIRST_NODE_VALUE).like(StrUtil.isNotBlank(keyword), Area::getName, keyword));
        List<AreaListVo> resultList = BeanUtil.copyToList(provinceList, AreaListVo.class);
        return R.ok(resultList);
    }


    @GetMapping("/child")
    @ApiOperation(value = "根据id 查询下级区域")
    public R getAreaByParentId(@RequestParam(required = false) Long id, @RequestParam(required = false) String keyword) {
        List<Area> provinceList = areaService.list(Wrappers.<Area>query().lambda()
                .eq(id != null, Area::getParentId, id)
                .eq(id == null, Area::getParentId, GlobalConstant.FIRST_NODE_VALUE)
                .and(StrUtil.isNotBlank(keyword), x -> {
                    x.like(StrUtil.isNotBlank(keyword), Area::getName, keyword).or();
                    x.like(StrUtil.isNotBlank(keyword), Area::getSimpleSpelling, keyword).or();
                    x.like(StrUtil.isNotBlank(keyword), Area::getQuickQuery, keyword);
                }));

        List<AreaListVo> resultList = BeanUtil.copyToList(provinceList, AreaListVo.class);
        return R.ok(resultList);
    }


    @GetMapping("/info")
    @ApiOperation(value = "根据id 获取详情信息")
    public R info(@RequestParam Long id) {
        Area area = areaService.getById(id);
        if (area == null) {
            R.error("找不到此区域!");
        }
        return R.ok(BeanUtil.toBean(area, AreaVo.class));
    }

    @GetMapping("/info/multi")
    @ApiOperation(value = "根据id 获取详情信息")
    public R infoMulti(@RequestParam String ids) {
        List<String> idList = StrUtil.split(ids, StringPool.COMMA);
        List<Area> areaList = areaService.listByIds(idList);
        return R.ok(BeanUtil.copyToList(areaList, AreaVo.class));
    }

    @PostMapping
    @ApiOperation(value = "新增")
    public R add(@Valid @RequestBody AddAreaDto dto) {
        long count = areaService.count(Wrappers.<Area>query().lambda().eq(Area::getCode, dto.getCode()));
        if (count > 0) {
            return R.error("区域编码已经存在！");
        }

        Area area = BeanUtil.toBean(dto, Area.class);
        return R.ok(areaService.save(area));
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public R update(@Valid @RequestBody UpdateAreaDto dto) {
        long count = areaService.count(Wrappers.<Area>query().lambda()
                .eq(Area::getCode, dto.getCode())
                .ne(Area::getId, dto.getId()));
        if (count > 0) {
            return R.error("区域编码已经存在！");
        }
        Area area = BeanUtil.toBean(dto, Area.class);

        return R.ok(areaService.updateById(area));
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    public R delete(@Valid @RequestBody List<Long> ids) {
        return R.ok(areaService.removeByIds(ids));
    }

}
