package cn.iocoder.yudao.module.inspection.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.inspection.entity.NweUsers;
import cn.iocoder.yudao.util.ExcelUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.iocoder.yudao.module.core.controller.BaseController;
import cn.iocoder.yudao.module.inspection.entity.SiteInfo;
import cn.iocoder.yudao.module.inspection.service.ISiteInfoService;
import cn.iocoder.yudao.module.core.page.TableDataInfo;

/**
 * inspectionController
 *
 * @author zcy
 * @date 2023-09-01
 */
@RestController
@RequestMapping("/admin-api/inspection/info")
public class SiteInfoController extends BaseController
{
    @Autowired
    private ISiteInfoService siteInfoService;

    /**
     * 查询inspection列表
     */
    @GetMapping("/list")
    @TenantIgnore
    public TableDataInfo list(SiteInfo siteInfo, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize)
    {
        Page<SiteInfo> page = new Page<>(pageNum, pageSize);
        List<SiteInfo> list = siteInfoService.selectSiteInfoList(page,siteInfo);
        return getDataTable(list);
    }

    /**
     * 导出inspection列表
     */
    @PreAuthorize("@ss.hasPermi('inspection:info:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SiteInfo siteInfo)
    {
        Page<SiteInfo> page = new Page<>();
        List<SiteInfo> list = siteInfoService.selectSiteInfoList(page,siteInfo);
        ExcelUtil<SiteInfo> util = new ExcelUtil<SiteInfo>(SiteInfo.class);
        util.exportExcel(response, list, "inspection数据");
    }

    /**
     * 获取inspection详细信息
     */
//    @PreAuthorize("@ss.hasPermi('inspection:info:query')")
    @GetMapping(value = "/{id}")
    public CommonResult getInfo(@PathVariable("id") Long id)
    {
        return success(siteInfoService.selectSiteInfoById(id));
    }

    /**
     * 新增inspection
     */
//    @PreAuthorize("@ss.hasPermi('inspection:info:add')")
    @PostMapping
    public CommonResult add(@RequestBody SiteInfo siteInfo)
    {
        return toAjax(siteInfoService.insertSiteInfo(siteInfo));
    }

    /**
     * 修改inspection
     */
//    @PreAuthorize("@ss.hasPermi('inspection:info:edit')")
    @PutMapping
    public CommonResult edit(@RequestBody SiteInfo siteInfo)
    {
        return toAjax(siteInfoService.updateSiteInfo(siteInfo));
    }

    /**
     * 删除inspection
     */
//    @PreAuthorize("@ss.hasPermi('inspection:info:remove')")
	@DeleteMapping("/{ids}")
    public CommonResult remove(@PathVariable Long[] ids)
    {
        return toAjax(siteInfoService.deleteSiteInfoByIds(ids));
    }

    /**
     * 迁移数据库：查询老表数据，并且将数据插入到新表中
     */
    @GetMapping("/transferDB")
    public CommonResult transferDB(){

        int sum = 0;

        // 获取老数据表的信息，类型为JSONObject
        List<JSONObject> jsonObjects = siteInfoService.transferDB();

        // 遍历从数据库获取的 JSON 对象列表
        for (JSONObject userObject : jsonObjects) {
            // 将 JSON 对象转换为 NweUsers 实例
            NweUsers user = userObject.toJavaObject(NweUsers.class);

            // 使用 BeanUtils 将 JSON 对象的属性复制到 NweUsers 实例中
            BeanUtils.copyProperties(userObject,user);

            // 将 NweUsers 实例插入到数据库中，并获取插入操作的结果
            sum += siteInfoService.addNewDB(user);
        }
        return success(sum);
    }

}
