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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.core.metadata.IPage;
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;


/**
 * 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 CommonResult list(SiteInfo siteInfo, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize)
    {
        Page<SiteInfo> page = new Page<>(pageNum, pageSize);
        IPage<SiteInfo> list = siteInfoService.selectSiteInfoList(page,siteInfo);
        return success(list);
    }

    /**
     * 导出inspection列表
     */
    @PreAuthorize("@ss.hasPermi('inspection:info:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SiteInfo siteInfo,
                       @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                       @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize)
    {
        Page<SiteInfo> page = new Page<>();
        IPage<SiteInfo> list = siteInfoService.selectSiteInfoList(page,siteInfo);
        ExcelUtil<SiteInfo> util = new ExcelUtil<SiteInfo>(SiteInfo.class);
        util.exportExcel(response, list.getRecords(), "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);
    }

    /**
     * 修改user_id
     */
    @GetMapping("/updateUserID")
    public CommonResult updateUserId(){
        //Map key存放老Id，value存放新Id
        Map<Integer, Integer> map = new HashMap<>();
        map.put(135,4826);
        map.put(855,4828);
        map.put(896,4829);
        map.put(900,4830);
        map.put(901,4221);
        map.put(902,4832);
        map.put(903,123);
        map.put(904,846);
        map.put(905,125);
        map.put(907,850);
        map.put(908,845);
        map.put(909,4568);
        map.put(910,4839);
        map.put(911,4840);
        map.put(912,4841);
        map.put(913,4842);
        map.put(914,4843);
        map.put(915,4844);
        map.put(916,4845);
        map.put(917,4846);
        map.put(918,4847);
        map.put(919,138);
        map.put(920,4849);
        map.put(921,4850);
        map.put(924,4851);
        map.put(926,4853);
        map.put(927,2793);
        map.put(928,4855);
        map.put(929,842);
        map.put(930,4857);
        map.put(931,4858);
        map.put(932,908);
        map.put(933,4860);
        map.put(934,4861);
        map.put(935,4862);
        map.put(936,4863);

        int sum = 0;
        //遍历Map，获得key和value
        for (Map.Entry<Integer, Integer> userId : map.entrySet()) {
            Integer oldUserId = userId.getKey();
            Integer newUserId = userId.getValue();
            sum +=siteInfoService.updateUserId1(newUserId ,oldUserId);
            sum +=siteInfoService.updateUserId2(newUserId ,oldUserId);
            sum +=siteInfoService.updateUserId3(newUserId ,oldUserId);
            sum +=siteInfoService.updateUserId4(newUserId ,oldUserId);
            sum +=siteInfoService.updateUserId5(newUserId ,oldUserId);
        }

        return success(sum);
    }


}
