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.bz.BzMedia;
import com.zhiqiyun.open.core.models.place.PlaceCategory;
import com.zhiqiyun.open.core.models.user.OauthInfo;
import com.zhiqiyun.open.core.service.BzService;
import com.zhiqiyun.open.core.service.OauthService;
import com.zhiqiyun.open.core.service.PlaceCategoryService;
import com.zhiqiyun.open.mvc.Result;
import com.zhiqiyun.open.mvc.params.bz.QueryBzMediaParam;
import com.zhiqiyun.open.mvc.params.bz.SaveBzMediaParam;
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("/bz")
public class BzController {

    @Autowired
    private BzService bzService;

    @Autowired
    private OauthService oauthService;

    @Autowired
    private PlaceCategoryService categoryService;


    @Permission(value = "bz.media", tags = "查询多媒体资源")
    @PostMapping("/findMediaPage")
    public Result findMediaPage(@RequestBody QueryBzMediaParam params) {

        QueryWrapper<BzMedia> queryWrapper = new QueryWrapper<>();
        if (params.getCategoryId() != null) {
            queryWrapper.eq("category_id", params.getCategoryId());
        }
        if (StringUtils.isNotBlank(params.getName())) {
            queryWrapper.like("name", params.getName());
        }

        Page<BzMedia> page = params.getPage();
        page.addOrder(OrderItem.desc("created_time"));

        Page<BzMedia> resultData = this.bzService.findMediaPage(page, queryWrapper);
        for (BzMedia media : page.getRecords()) {
            PlaceCategory category = this.categoryService.getById(media.getCategoryId());
            if (category != null) {
                media.setCategory(category);
            }

        }

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

    @Permission(value = "bz.media", tags = "添加多媒体资源")
    @PostMapping("/saveMedia")
    public Result saveMedia(@Valid @RequestBody SaveBzMediaParam param) throws Exception {
        OauthInfo oauthInfo = this.oauthService.getAuth(ServletContext.getAccessToken());

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

        entity.setCreatedBy(oauthInfo.getId());
        entity.setUpdatedBy(oauthInfo.getId());

        this.bzService.insertOrUpdateMedia(entity);

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

    @Permission(value = "bz.media", tags = "修改多媒体资源")
    @PostMapping("/updateMediaId")
    public Result updateMediaId(Long id, @Valid @RequestBody SaveBzMediaParam param) throws Exception {
        OauthInfo oauthInfo = this.oauthService.getAuth(ServletContext.getAccessToken());

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

        entity.setUpdatedBy(oauthInfo.getId());
        entity.setId(id);

        this.bzService.insertOrUpdateMedia(entity);

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

    @Permission(value = "bz.media", tags = "删除多媒体资源")
    @PostMapping("/deleteMediaByIds")
    public Result deleteMediaByIds(@RequestBody List<Long> ids) {
        this.bzService.deleteMediaByIds(ids);
        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }
}
