package com.nantian.cloud.jljc.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nantian.cloud.jljc.entity.TSupervisionQuestions;
import com.nantian.cloud.jljc.service.TSupervisionQuestionsService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 调查（询问）笔录问题及答案
 * @since 2020-10-29
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tSupervisionQuestions")
@Api(value = "tSupervisionQuestions", tags = "调查（询问）笔录问题及答案")
public class TSupervisionQuestionsController {

    private final TSupervisionQuestionsService tSupervisionQuestionsService;

    /**
     * 查询全部调查（询问）笔录问题及答案
     */
    @GetMapping("/list")
    @SysLog("查询全部调查（询问）笔录问题及答案")
    @ApiOperation(value = "查询全部调查（询问）笔录问题及答案", notes = "查询全部调查（询问）笔录问题及答案")
    @ApiImplicitParam(name = "TSupervisionQuestions", value = "调查（询问）笔录问题及答案对象", required = false, dataType = "json", paramType = "path")
    public R selectAll(TSupervisionQuestions tSupervisionQuestions) {
        tSupervisionQuestions.setDelFlag("1"); //筛选未删除项
        return R.ok(tSupervisionQuestionsService.list(Wrappers.query(tSupervisionQuestions)));
    }

    /**
     * 根据id获取调查（询问）笔录问题及答案
     */
    @GetMapping("/getById")
    @SysLog("根据id获取调查（询问）笔录问题及答案")
    @ApiOperation(value = "根据id获取调查（询问）笔录问题及答案", notes = "根据id获取调查（询问）笔录问题及答案")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R getById(Integer id) {
        return R.ok(tSupervisionQuestionsService.getById(id));
    }

    /**
     * 保存/修改调查（询问）笔录问题及答案
     */
    @PostMapping("saveOrUpdate")
    @SysLog("保存/修改调查（询问）笔录问题及答案")
    @ApiOperation(value = "保存/修改调查（询问）笔录问题及答案", notes = "保存/修改调查（询问）笔录问题及答案")
    @ApiImplicitParam(name = "TSupervisionQuestions", value = "调查（询问）笔录问题及答案对象", required = true, dataType = "json", paramType = "path")
    public R saveOrUpdate(@RequestBody TSupervisionQuestions tSupervisionQuestions) {
        ScUser u = SecurityUtils.getUser();
        tSupervisionQuestions.setUserid(u.getId());
        tSupervisionQuestions.setUpdateTime(new Date());
        if (tSupervisionQuestions.getId() != null) {
            tSupervisionQuestions.setDelFlag("1");
            tSupervisionQuestions.setDeptId(u.getDeptId());
        }
        return R.ok(tSupervisionQuestionsService.saveOrUpdate(tSupervisionQuestions));
    }

    /**
     * 批量保存/修改调查（询问）笔录问题及答案
     */
    @PostMapping("saveOrUpdateBatch")
    @SysLog("批量保存/修改调查（询问）笔录问题及答案")
    @ApiOperation(value = "批量保存/修改调查（询问）笔录问题及答案", notes = "批量保存/修改调查（询问）笔录问题及答案")
    @ApiImplicitParam(name = "data", value = "调查（询问）笔录问题及答案对象", required = true, dataType = "json", paramType = "path")
    public R saveOrUpdateBatch(@RequestBody String s) {
        JSONObject json = JSONObject.parseObject(s);
        String para = json.getString("data");
        List<TSupervisionQuestions> tSupervisionQuestionsList = (List<TSupervisionQuestions>) JSONArray.parseArray(para, TSupervisionQuestions.class);
        List<TSupervisionQuestions> newList = new ArrayList<>();
        for (TSupervisionQuestions tSupervisionQuestions : tSupervisionQuestionsList) {
            ScUser u = SecurityUtils.getUser();
            tSupervisionQuestions.setUserid(u.getId());
            tSupervisionQuestions.setUpdateTime(new Date());
            if (tSupervisionQuestions.getId() != null) {
                tSupervisionQuestions.setDelFlag("1");
                tSupervisionQuestions.setDeptId(u.getDeptId());
            }
            newList.add(tSupervisionQuestions);
        }
        //先删除之前所有的问答
        if(CollectionUtil.isNotEmpty(tSupervisionQuestionsList)){
            TSupervisionQuestions tSupervisionQuestions = tSupervisionQuestionsList.get(0);
            String monitorId = tSupervisionQuestions.getMonitorId();
            if(StringUtils.isNotEmpty(monitorId)){
                tSupervisionQuestionsService.removeByMonitorId(monitorId);
            }
        }
        return R.ok(tSupervisionQuestionsService.saveOrUpdateBatch(newList));
    }

    /**
     * 根据id删除调查（询问）笔录问题及答案
     */
    @GetMapping("/delete")
    @SysLog("根据id删除调查（询问）笔录问题及答案")
    @ApiOperation(value = "根据id删除调查（询问）笔录问题及答案", notes = "根据id删除调查（询问）笔录问题及答案")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R delete(Integer id) {
        TSupervisionQuestions tSupervisionQuestions = tSupervisionQuestionsService.getById(id);
        if (tSupervisionQuestions == null) {
            return R.failed("id不存在，删除调查（询问）笔录问题及答案失败！");
        }
        ScUser u = SecurityUtils.getUser();
        tSupervisionQuestions.setUpdateTime(new Date());
        tSupervisionQuestions.setDelFlag("4");
        tSupervisionQuestions.setUserid(u.getId());
        return R.ok(tSupervisionQuestionsService.updateById(tSupervisionQuestions));
    }
}
