/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.manager.dto.WbsFormElementDTO;
import org.springblade.manager.entity.WbsFormInitEntity;
import org.springblade.manager.mapper.WbsFormElementMapper;
import org.springblade.manager.service.IWbsFormElementService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.manager.entity.WbsFormElement;
import org.springblade.manager.vo.WbsFormElementVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * wbs表单元素 服务实现类
 *
 * @author liuyc
 * @since 2022-04-27
 */
@Service
public class WbsFormElementServiceImpl extends BaseServiceImpl<WbsFormElementMapper, WbsFormElement> implements IWbsFormElementService {

    @Resource
    private WbsFormElementMapper wbsFormElementMapper;


    @Override
    public IPage<WbsFormElementVO> selectWbsFormElementPage(IPage<WbsFormElementVO> page, WbsFormElementVO wbsFormElement) {
        return page.setRecords(baseMapper.selectWbsFormElementPage(page, wbsFormElement));
    }

    @Override
    public boolean addFormElement(WbsFormElement wbsFormElement) {
        int insert1 = baseMapper.insert(wbsFormElement);
        if (insert1 > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean selectElementsById(List<WbsFormElement> wbsFormElementList) {
        List<Long> ids = new ArrayList<>();
        for (WbsFormElement wbsFormElement : wbsFormElementList) {
            Long id = wbsFormElement.getId();
            ids.add(id);
        }
        List<WbsFormElement> wbsFormElements = baseMapper.selectBatchIds(ids);

        if ((wbsFormElements).equals(wbsFormElementList)) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean initTable(List<WbsFormElement> elementList, String tableName) {
       /* List<WbsFormInitEntity> listDTO = new ArrayList<>();
        for (WbsFormElement wbsFormElement : elementList) {
            WbsFormInitEntity w = new WbsFormInitEntity();
            String eName = wbsFormElement.getEName();
            Integer eType = wbsFormElement.getEType();
            Integer eLength = wbsFormElement.getELength();
            w.setFieldName(eName);
            w.setFieldLength(String.valueOf(eType));
            w.setFieldLength(String.valueOf(eLength));
            listDTO.add(w);
        }*/
        //定义建表SQL
        String sql = createSQL(elementList);
        //建表
        Integer row = wbsFormElementMapper.createTable(sql, tableName);
//        System.out.println("row:///////////////////////////" + row);
        if (row >= 0) {
            return true;
        }
        return false;
    }

    private String createSQL(List<WbsFormElement> elementList) {
        // 创建 StringBuffer 拼接sql
        StringBuffer sbr = new StringBuffer();
        /*sbr.append("CREATE TABLE `m_wbs_form_" + tableName + DateUtil.time() + "` (\n");
        //判断类型
        sbr.append("`id` bigint(20) NOT NULL, \n");
        */
        for (WbsFormElement wbsFormElement : elementList) {
            //获取字段名、类型、长度
            String fieldName = wbsFormElement.getEName();
            String fieldType = judgeDataType(wbsFormElement.getEType());
            String fieldLength = String.valueOf(wbsFormElement.getELength());
            sbr.append("" + fieldName + "");
            if ("varchar".equals(fieldType)) {
                sbr.append(" " + fieldType + "(" + fieldLength + ") ,\n");
            } else if ("bigint".equals(fieldType)) {
                sbr.append(" " + fieldType + "(" + fieldLength + ") ,\n");
            } else if ("decimal".equals(fieldType)) {
                sbr.append(" " + fieldType + "(" + fieldLength + ") ,\n");
            } else if ("datetime".equals(fieldType)) {
                sbr.append(" " + fieldType + "(" + fieldLength + ") ,\n");
            }
        }
        /*sbr.append(" PRIMARY KEY (`id`) USING BTREE )\n");
        sbr.append(" ENGINE=InnoDB DEFAULT CHARSET=utf8 \n");*/
        /*System.out.println("SQL:******* " + sbr);*/
        return String.valueOf(sbr);
    }

    /**
     * 判断类型
     */
    private String judgeDataType(Integer type) {
        switch (type) {
            case 1:
                return "varchar";
            case 2:
                return "bigint";
            case 3:
                return "decimal";
            case 4:
                return "datetime";
            default:
                return "varchar";
        }
    }


    @Override
    public List<WbsFormElement> selectElementListByFid(String tableId) {
        QueryWrapper<WbsFormElement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_id", tableId);
        List<WbsFormElement> wbsFormElements = baseMapper.selectList(queryWrapper);
        return wbsFormElements;
    }


}
