package com.zhiqiyun.open.mvc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiqiyun.open.annotation.Permission;
import com.zhiqiyun.open.core.models.place.PlaceCategoryExtendColumn;
import com.zhiqiyun.open.core.service.PlaceCategoryExtendColumnService;
import com.zhiqiyun.open.core.service.SequenceService;
import com.zhiqiyun.open.mvc.Result;
import com.zhiqiyun.open.mvc.params.place.QueryPlaceCategoryExtendColumnParam;
import com.zhiqiyun.open.mvc.params.place.SavePlaceCategoryExtendColumnParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/bz/data/type/extend/column")
public class PlaceCategoryExtendColumnController {

    @Autowired
    private PlaceCategoryExtendColumnService placeCategoryExtendColumnService;

    @Autowired
    private SequenceService sequenceService;

    @Permission(value = "place.category.extend.column.find", tags = "查询文旅场所分类扩展字段")
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPlaceCategoryExtendColumnParam param) {

        QueryWrapper<PlaceCategoryExtendColumn> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(param.getFieldKey())) {
            wrapper.eq("field_key", param.getFieldKey());
        }
        if (StringUtils.isNotBlank(param.getFieldLabel())) {
            wrapper.like("field_label", param.getFieldLabel());
        }
        Page<PlaceCategoryExtendColumn> page = param.getPage();
        page.addOrder(OrderItem.asc("sort_number"));
        page.addOrder(OrderItem.desc("id"));

        Page<PlaceCategoryExtendColumn> resultData = this.placeCategoryExtendColumnService.page(page, wrapper);

        return Result.instance(Result.Code.SUCCESS).setData(resultData);
    }

    @Permission(value = "place.category.extend.column.add", tags = "新建文旅场所分类扩展字段")
    @PostMapping("/save")
    public Result save(@Valid @RequestBody SavePlaceCategoryExtendColumnParam param) throws Exception {

        PlaceCategoryExtendColumn entity = new PlaceCategoryExtendColumn();
        BeanUtils.copyProperties(param, entity);

        if (param.getSortNumber() == null) {
            entity.setSortNumber(0);
        }

        entity.setId(this.sequenceService.nextId());

        this.placeCategoryExtendColumnService.save(entity);

        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }

    @Permission(value = "place.category.extend.column.edit", tags = "更新文旅场所分类扩展字段")
    @PostMapping("/updateById")
    public Result updateById(Long id, @Valid @RequestBody SavePlaceCategoryExtendColumnParam param) {
        PlaceCategoryExtendColumn entity = new PlaceCategoryExtendColumn();
        BeanUtils.copyProperties(param, entity);

        if (param.getSortNumber() == null) {
            entity.setSortNumber(0);
        }

        entity.setId(id);

        this.placeCategoryExtendColumnService.updateById(entity);

        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }

    @Permission(value = "place.category.extend.column.delete", tags = "删除文旅场所分类扩展字段")
    @PostMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Long> ids) {
        this.placeCategoryExtendColumnService.removeByIds(ids);
        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }
}
