package com.sptg.video.cloud.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.video.cloud.api.dto.PresetInfoDto;
import com.sptg.video.cloud.api.entity.PresetInfo;
import com.sptg.video.cloud.api.entity.SysParams;
import com.sptg.video.cloud.api.feigns.PresetInfoFeign;
import com.sptg.video.cloud.biz.service.PresetInfoServiceInterface;
import com.sptg.video.cloud.biz.service.VCSysParamsServiceInterface;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zwx
 * @create 2020/10/18 16:40
 */
@RequiredArgsConstructor
@RestController
@Slf4j
public class PresetInfoController implements PresetInfoFeign {

    private final PresetInfoServiceInterface presetInfoServiceInterface;
    private final VCSysParamsServiceInterface sysParamsServiceInterface;

    @Override
    @PostMapping(value = "/vcPresetInfo/showPresetInfoList")
    public ResultBean<Page<PresetInfo>> showPresetInfoList(@RequestBody PresetInfoDto presetInfoDto) {
        List<PresetInfo> presetInfoList = this.presetInfoServiceInterface.showPresetInfoList(presetInfoDto);
        ResultBean<Page<PresetInfo>> pageResultBean = new ResultBean<>();
        Page<PresetInfo> page = new Page<>();
        page.setRecords(presetInfoList);
        pageResultBean.setData(page);
        return pageResultBean;
    }

    @Override
    @GetMapping(value = "/vcPresetInfo/findPresetInfoById/{id}")
    public PresetInfo findPresetInfoById(@PathVariable(value = "id") Long id) {
        return this.presetInfoServiceInterface.selectById(id);
    }

    @Override
    @PostMapping(value = "/vcPresetInfo/sync/list")
    public ResultBean<Boolean> syncPresetInfoList(@RequestBody PresetInfoDto presetInfoDto) {
        SysParams sysParams = sysParamsServiceInterface.findSysParamsById(1L);
        List<PresetInfo> presetInfoList = this.presetInfoServiceInterface.syncPresetInfoList(sysParams, presetInfoDto);
        if(presetInfoList != null && presetInfoList.size() > 0){
            presetInfoList.forEach(source -> {
                PresetInfo target = null;
                target = this.presetInfoServiceInterface.findPresetInfoById(source.getId());
                if(target == null){
                    target = new PresetInfo();
                }else{
                    source.setId(target.getId());
                }
                BeanUtil.copyProperties(source, target);
                this.presetInfoServiceInterface.insertOrUpdate(target);
            });
        }
        return new ResultBean<>(ResultEnum.SUCCESS, true);
    }
}
