package com.ruoyi.interfaces.controller;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.interfaces.core.page.Page;
import com.ruoyi.interfaces.core.page.PageUtils;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.interfaces.domain.PtDataExp;
import com.ruoyi.interfaces.domain.PtDataExpField;
import com.ruoyi.interfaces.domain.SysCate;
import com.ruoyi.interfaces.mapper.PtDataExpMapper;
import com.ruoyi.interfaces.service.SysCateService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/pt/data/exp")
public class PtDataExpController extends BaseController {

    @Autowired
    private PtDataExpMapper ptDataExpMapper;

    @Autowired
    private SysCateService sysCateService;

    @ApiOperation("添加数据导出")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public AjaxResult insert(PtDataExp p, String json) throws Exception {
        p.setId(IdUtils.simpleUUID());
        int insert = ptDataExpMapper.insert(p);
        if (insert > 0) {
            //添加详情
            if (json != null && !"".equals(json)) {
                String[] strs = json.split(";");
                for (String str : strs) {
                    PtDataExpField ptDataExpField = JSONObject.parseObject(str, PtDataExpField.class);
                    ptDataExpField.setId(p.getId());
                    ptDataExpField.setFid(IdUtils.simpleUUID());
                    ptDataExpMapper.insertPtDataExpField(ptDataExpField);
                }
            }
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @ApiOperation("查询数据导出")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Page selectAll(int page, int rows, PtDataExp ptDataExp) {
        PageHelper.startPage(page, rows);
        List<PtDataExp> list = ptDataExpMapper.selectAll(ptDataExp);
        PageInfo<PtDataExp> pageInfo = new PageInfo<>(list);
        return PageUtils.convert(pageInfo);
    }

    @ApiOperation("修改数据导出")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public AjaxResult update(PtDataExp p, String json) {
        int update = ptDataExpMapper.update(p);
        PtDataExpField ptDataExpField2 = new PtDataExpField();
        ptDataExpField2.setId(p.getId());
        if (update > 0) {
            ptDataExpMapper.deletePtDataExpField(ptDataExpField2);
            if (json != null && !"".equals(json)) {
                String[] strs = json.split(";");
                for (String str : strs) {
                    System.out.println(str);
                    PtDataExpField ptDataExpField = JSONObject.parseObject(str, PtDataExpField.class);
                    ptDataExpField.setId(p.getId());
                    ptDataExpField.setFid(IdUtils.simpleUUID());
                    ptDataExpMapper.insertPtDataExpField(ptDataExpField);
                }
            }
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @ApiOperation("删除数据导出")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public AjaxResult delete(String ids) {
        String[] strs = ids.split(",");
        int deletes = 0;
        for (String str : strs) {
            //查询是否存在子类
            PtDataExpField pt = new PtDataExpField();
            pt.setId(str);
            ptDataExpMapper.deletePtDataExpField(pt);
            deletes += ptDataExpMapper.delete(str);
        }
        if (deletes == strs.length) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @ApiOperation("删除操作")
    @RequestMapping(value = "/delete_field", method = RequestMethod.GET)
    public AjaxResult deleteField(PtDataExpField pf) {
        int delete = ptDataExpMapper.deletePtDataExpField(pf);
        if (delete > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @ApiOperation("跳到数据导出修改页面")
    @RequestMapping(value = "/to_update", method = RequestMethod.GET)
    public ModelAndView toUpdate(String id) {
        ModelAndView model = new ModelAndView();
        model.addObject("ptDataExp", ptDataExpMapper.findByOne(id));
        model.setViewName("mng/pt_data_exp_edit");
        return model;
    }

    @ApiOperation("根据数据导出查询导出字段")
    @RequestMapping(value = "/select_field", method = RequestMethod.POST)
    public List<PtDataExpField> selectField(PtDataExpField p) {
        List<PtDataExpField> list = ptDataExpMapper.selectAllPtDataExpField(p);
        return list;
    }

    @ApiOperation("数据列表")
    @RequestMapping(value = "/cate_list", method = RequestMethod.GET)
    public List<SysCate> list(String tabName) {
        SysCate criteria = new SysCate();
        criteria.setCateTable(tabName);
        return sysCateService.findAllO(criteria);//.selectByCriteria(criteria);//查询
    }
}
