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.DistrictNodeDto;
import com.sptg.video.cloud.api.entity.DistrictNode;
import com.sptg.video.cloud.api.entity.SysParams;
import com.sptg.video.cloud.api.feigns.DistrictNodeFeign;
import com.sptg.video.cloud.biz.service.DistrictNodeServiceInterface;
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 12:14
 */
@RequiredArgsConstructor
@RestController
@Slf4j
public class DistrictNodeController implements DistrictNodeFeign {

    private final DistrictNodeServiceInterface districtNodeServiceInterface;

    private final VCSysParamsServiceInterface sysParamsServiceInterface;

    @Override
    @PostMapping(value = "/vcDistrictNode/showDistrictNodeList")
    public ResultBean<Page<DistrictNode>> showDistrictNodeList(@RequestBody DistrictNodeDto districtNodeDto) {
        List<DistrictNode> districtNodeList = districtNodeServiceInterface.showDistrictNodeList(districtNodeDto);
        ResultBean<Page<DistrictNode>> pageResultBean = new ResultBean<>();
        Page<DistrictNode> page = new Page<>();
        page.setRecords(districtNodeList);
        pageResultBean.setData(page);
        return pageResultBean;
    }

    @Override
    @GetMapping(value = "/vcDistrictNode/findDistrictNodeById/{id}")
    public DistrictNode findDistrictNodeById(@PathVariable(value = "id") Long id) {
        return this.districtNodeServiceInterface.selectById(id);
    }

    @Override
    @PostMapping(value = "/vcDistrictNode/sync/list")
    public ResultBean<Boolean> syncDistrictNodeList(@RequestBody DistrictNodeDto districtNodeDto) {
        SysParams sysParams = sysParamsServiceInterface.findSysParamsById(1L);
        List<DistrictNode> districtNodeList = this.districtNodeServiceInterface.syncDistrictNodeList(sysParams, districtNodeDto);
        if(districtNodeList != null && districtNodeList.size() > 0){
            districtNodeList.forEach(source -> {
                DistrictNode target = null;
                target = this.districtNodeServiceInterface.findByDistrictId(source.getId());
                if(target == null){
                    target = new DistrictNode();
                }else{
                    source.setLId(target.getLId());
                }
                BeanUtil.copyProperties(source, target);
                this.districtNodeServiceInterface.insertOrUpdate(target);
            });
        }
        return new ResultBean<>(ResultEnum.SUCCESS, true);
    }
}
