package com.persagy.dmp.rwd.migrate.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.persagy.dmp.basic.model.QueryCriteria;
import com.persagy.dmp.basic.utils.QueryCriteriaHelper;
import com.persagy.dmp.common.constant.CommonConstant;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.context.AppContext;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.common.utils.ParamCheckUtil;
import com.persagy.dmp.common.utils.ResultHelper;
import com.persagy.dmp.mybatis.utils.ConditionUtil;
import com.persagy.dmp.rwd.migrate.entity.SyncData;
import com.persagy.dmp.rwd.migrate.service.ISyncDataService;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * @author lvxianyun
 * @title: DigitalMigrateLogController
 * @projectName digital-twin
 * @description: adm 数据一键迁移到项目现场 日志记录
 * @date 2021/12/20 16:35
 */

@Slf4j
@RestController
@RequestMapping("/log/migrate")
public class DigitalMigrateLogController {
    @Autowired
    private ISyncDataService service;

    @PostMapping("/query")
    public CommonResult<List<SyncData>> query(@RequestBody QueryCriteria criteria) {
        if(criteria == null) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);

        QueryWrapper<SyncData> wrapper = new QueryWrapper<>();
        // 添加所属项目条件
        ConditionUtil.ensureProjectCriteriaDirectly(wrapper);
        // 转换查询条件
        QueryCriteriaHelper.toWrapper(wrapper, criteria, SyncData.class);
        Page page = QueryCriteriaHelper.toPage(criteria);
        if(criteria.isOnlyCount()) {
            int total = service.count(wrapper);
            page.setTotal(total);
        } else {
            page = service.page(QueryCriteriaHelper.toPage(criteria), wrapper);
        }
        return ResultHelper.multi(page.getRecords(), page.getTotal());
    }

    @PostMapping("/create")
    public CommonResult<List<SyncData>> createBatch(@Valid @RequestBody List<SyncData> voList){
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
        String projectId = AppContext.getContext().getProjectId();
        ensureDefaultValue(voList,projectId);
        try {
            service.save(voList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //批量新增迁移日志
        return ResultHelper.multi(voList);
    }

    /**
     * 补充默认值
     * @param voList
     */
    private void ensureDefaultValue(List<SyncData> voList,String projectId) {
        for (SyncData vo : voList) {
            if(StrUtil.isBlank(vo.getId())) {
                vo.setId(IdUtil.fastSimpleUUID());
            }
            if(StrUtil.isBlank(vo.getProjectId())) {
                vo.setProjectId(projectId);
            }
            if(vo.getValid() == null) {
                vo.setValid(1);
            }
        }
    }
}
