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.CruiseInfoDto;
import com.sptg.video.cloud.api.entity.CruiseInfo;
import com.sptg.video.cloud.api.entity.SysParams;
import com.sptg.video.cloud.api.feigns.CruiseInfoFeign;
import com.sptg.video.cloud.biz.service.CruiseInfoServiceInterface;
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 17:03
 */
@RequiredArgsConstructor
@RestController
@Slf4j
public class CruiseInfoController implements CruiseInfoFeign {

    private final CruiseInfoServiceInterface cruiseInfoServiceInterface;
    private final VCSysParamsServiceInterface sysParamsServiceInterface;

    @Override
    @PostMapping(value = "/vcCruiseInfo/showCruiseInfoList")
    public ResultBean<Page<CruiseInfo>> showCruiseInfoList(@RequestBody CruiseInfoDto cruiseInfoDto) {
        List<CruiseInfo> cruiseInfoList = this.cruiseInfoServiceInterface.showCruiseInfoList(cruiseInfoDto);
        ResultBean<Page<CruiseInfo>> pageResultBean = new ResultBean<>();
        Page<CruiseInfo> page = new Page<>();
        page.setRecords(cruiseInfoList);
        pageResultBean.setData(page);
        return pageResultBean;
    }

    @Override
    @GetMapping(value = "/vcCruiseInfo/findCruiseInfoById/{id}")
    public CruiseInfo findCruiseInfoById(@PathVariable(value = "id") Long id) {
        return this.cruiseInfoServiceInterface.selectById(id);
    }

    @Override
    @PostMapping(value = "/vcCruiseInfo/sync/list")
    public ResultBean<Boolean> syncCruiseInfoList(@RequestBody CruiseInfoDto cruiseInfoDto) {
        SysParams sysParams = sysParamsServiceInterface.findSysParamsById(1L);
        List<CruiseInfo> cruiseInfoList = this.cruiseInfoServiceInterface.syncCruiseInfoList(sysParams, cruiseInfoDto);
        if(cruiseInfoList != null && cruiseInfoList.size() > 0){
            cruiseInfoList.forEach(source -> {
                CruiseInfo target = null;
                target = this.cruiseInfoServiceInterface.findCruiseInfoByCruiseGr(source.getCruiseGr());
                if(target == null){
                    target = new CruiseInfo();
                }else{
                    source.setId(target.getId());
                }
                BeanUtil.copyProperties(source, target);
                this.cruiseInfoServiceInterface.insertOrUpdate(target);
            });
        }
        return new ResultBean<>(ResultEnum.SUCCESS, true);
    }
}
