package com.th.demo.service.impl.develop;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.th.demo.mapping.develop.DevTablePropertyMapper;
import com.th.demo.mapping.system.SysDicMapper;
import com.th.demo.model.develop.DevTableProperty;
import com.th.demo.model.system.ImportTest;
import com.th.demo.model.system.SysDic;
import com.th.demo.model.system.Type;
import com.th.demo.service.develop.DevTablePropertyService;
import com.th.demo.service.system.SysDicService;
import com.th.demo.tools.ClassTool;
import com.th.demo.tools.ExcelTools;
import com.th.demo.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * Created by dell on 2017-07-13.
 */
@Service("DevTablePropertyService")
public class DevTablePropertyServiceImpl implements DevTablePropertyService {


    @Autowired
    public DevTablePropertyMapper devTablePropertyMapper;

    int num = 0;
    String result=Type.FAIL;
    Gson gson=new GsonBuilder().setDateFormat("yyyy-MM-dd hh:mm:ss").create();//gson json的三方框架 数据传递格式

    @Override
    public String query(int page, int rows, String tableLabel) {
        try {
            PageHelper.startPage(page, rows);
            List<DevTableProperty> list = new ArrayList<DevTableProperty>();
            list = devTablePropertyMapper.select(tableLabel);
            //查询当前分页数据
            PageInfo<DevTableProperty> pageInfo = new PageInfo<>(list);
            result = gson.toJson(pageInfo);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return result;
        }
    }

    @Override
    public String save(String id, String tableName, String tableField, String tableType, String tableLabel, String tableProp,
        String tableWidth, String isForm, String formType, String formSort, String isQuery, String queryType,
        String querySort, String userId) {
        DevTableProperty devTableProperty;
        try {
    if ( id.equals("")||id.equals(null)) {
        devTableProperty = new DevTableProperty();
        devTableProperty.setTableName(tableName);
        devTableProperty.setTableField(tableField);
        devTableProperty.setTableType(tableType);
        devTableProperty.setTableLabel(tableLabel);
        devTableProperty.setTableProp(tableProp);
        devTableProperty.setTableWidth(tableWidth);
        devTableProperty.setIsForm(isForm);
        devTableProperty.setFormType(formType);
        devTableProperty.setFormSort(formSort);
        devTableProperty.setIsQuery(isQuery);
        devTableProperty.setQueryType(queryType);
        devTableProperty.setQuerySort(querySort);
        devTableProperty.setCancelFlag("0");
        devTableProperty.setAddId(userId);
        devTableProperty.setAddTime(new Date());
        num = devTablePropertyMapper.insert(devTableProperty);
        result = Tools.moreThanZero(num);
    } else {
        devTableProperty = devTablePropertyMapper.selectByPrimaryKey(id);
        devTableProperty.setTableName(tableName);
        devTableProperty.setTableField(tableField);
        devTableProperty.setTableType(tableType);
        devTableProperty.setTableLabel(tableLabel);
        devTableProperty.setTableProp(tableProp);
        devTableProperty.setTableWidth(tableWidth);
        devTableProperty.setIsForm(isForm);
        devTableProperty.setFormType(formType);
        devTableProperty.setFormSort(formSort);
        devTableProperty.setIsQuery(isQuery);
        devTableProperty.setQueryType(queryType);
        devTableProperty.setQuerySort(querySort);
        devTableProperty.setModifyId(userId);
        devTableProperty.setModifyTime(new Date());
        num = devTablePropertyMapper.updateByPrimaryKey(devTableProperty);
        result = Tools.moreThanZero(num);
    }
        return result;
    } catch (Exception ex) {
        ex.printStackTrace();
        return result;
    }
    }

    @Override
    public String remove(String id,String userId) {
        DevTableProperty devTableProperty = new DevTableProperty();
        devTableProperty.setId(id);
        devTableProperty.setCancelId(userId);
        devTableProperty.setCancelTime(new Date());
        devTableProperty.setCancelFlag("9");
        num = devTablePropertyMapper.deleteByUpdate9(devTableProperty);
        result = Tools.moreThanZero(num);
        return result;
    }
}
