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.category.CategoryOccupation;
import com.zhiqiyun.open.core.models.user.OauthInfo;
import com.zhiqiyun.open.core.service.CategoryOccupationService;
import com.zhiqiyun.open.core.service.OauthService;
import com.zhiqiyun.open.core.service.SequenceService;
import com.zhiqiyun.open.mvc.Result;
import com.zhiqiyun.open.mvc.params.category.QueryCategoryOccupationParam;
import com.zhiqiyun.open.mvc.params.category.SaveCategoryOccupationParam;
import com.zhiqiyun.open.utils.DateUtil;
import com.zhiqiyun.open.utils.ServletContext;
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("/category/occupation")
public class CategoryOccupationController {

    @Autowired
    private CategoryOccupationService categoryOccupationService;

    @Autowired
    private SequenceService sequenceService;

    @Autowired
    private OauthService oauthService;

    @Permission(value = "category.occupation.find", tags = "查询文旅岗位目录")
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryCategoryOccupationParam param) {

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

        if (StringUtils.isNotBlank(param.getName())) {
            wrapper.like("name", param.getName());
        }
        Page<CategoryOccupation> page = param.getPage();
        page.addOrder(OrderItem.asc("sort_number"));
        page.addOrder(OrderItem.desc("id"));

        Page<CategoryOccupation> resultData = this.categoryOccupationService.page(page, wrapper);

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

    @Permission(value = "category.occupation.add", tags = "新建文旅岗位目录")
    @PostMapping("/save")
    public Result save(@Valid @RequestBody SaveCategoryOccupationParam param) throws Exception {

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

        OauthInfo oauthInfo = this.oauthService.getAuth(ServletContext.getAccessToken());

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

        entity.setCreatedTime(DateUtil.current());
        entity.setCreatedBy(oauthInfo.getId());

        entity.setUpdatedTime(DateUtil.current());
        entity.setUpdatedBy(oauthInfo.getId());
        entity.setId(this.sequenceService.nextId());

        this.categoryOccupationService.save(entity);

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

    @Permission(value = "category.occupation.edit", tags = "更新文旅岗位目录")
    @PostMapping("/updateById")
    public Result updateById(Long id, @Valid @RequestBody SaveCategoryOccupationParam param) {
        CategoryOccupation entity = new CategoryOccupation();
        BeanUtils.copyProperties(param, entity);

        OauthInfo oauthInfo = this.oauthService.getAuth(ServletContext.getAccessToken());

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

        entity.setUpdatedBy(oauthInfo.getId());
        entity.setUpdatedTime(DateUtil.current());
        entity.setId(id);

        this.categoryOccupationService.updateById(entity);

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

    @Permission(value = "category.occupation.delete", tags = "删除文旅岗位目录")
    @PostMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Long> ids) {
        this.categoryOccupationService.removeByIds(ids);
        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }
}
