package com.zcxk.controller;

import com.zcxk.model.AjaxMessage;
import com.zcxk.model.LoginUser;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.SceneService;
import com.zcxk.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.*;

import org.springframework.web.bind.annotation.*;
import com.zcxk.entity.SceneTypeEntity;
import com.zcxk.service.SceneTypeService;

/**
 * 场景类型页面控制器
 * @author WYY
 * @date 2020-11-17 19:31
 */
@Controller
@RequestMapping("/scenetype")
@Api(tags = "场景类型管理")
public class SceneTypeController {

    @Autowired
    private SceneTypeService sceneTypeService;
    @Autowired
    private SceneService sceneService;

    @ApiOperation(value = "按父级节点查询子节点信息")
    @RequestMapping(value = "/selectListByParentId", method = RequestMethod.GET)
    @ResponseBody
    public AjaxMessage<List<SceneTypeEntity>> list(@ApiParam(value = "父级ID", required = true) @RequestParam Long id) {
        SceneTypeEntity sceneType = new SceneTypeEntity();
        sceneType.setParentScenetypeId(id);
        List<SceneTypeEntity> page = sceneTypeService.findListByParentID(sceneType);
        return new AjaxMessage<>(ResultStatus.OK, page);
    }

    @ApiOperation(value = "查询所有场景类型")
    @RequestMapping(value = "/selectList", method = RequestMethod.GET)
    @ResponseBody
    public AjaxMessage<List<SceneTypeEntity>> selectAllList(@ApiParam(value = "名称", required = false) @RequestParam(required = false) String sceneTypeName) {
        SceneTypeEntity sceneType = new SceneTypeEntity();
        sceneType.setSceneTypeName(sceneTypeName);
        List<SceneTypeEntity> page = sceneTypeService.findList(sceneType);
        return new AjaxMessage<>(ResultStatus.OK, page);
    }

    @ApiOperation(value = "查询场景类型树")
    @RequestMapping(value = "/selectTreeList", method = RequestMethod.GET)
    @ResponseBody
    public AjaxMessage<List<SceneTypeEntity>> selectTreeList(@ApiParam(value = "父级ID", required = false) @RequestParam(required = false) Long id) {
        SceneTypeEntity sceneType = new SceneTypeEntity();
        if (id == null) {
            id = 0L;
        }
        sceneType.setParentScenetypeId(id);
        List<SceneTypeEntity> page = sceneTypeService.findList2(sceneType);
        return new AjaxMessage<>(ResultStatus.OK, page);
    }

    /**
     * 新增
     */
    @ApiOperation(value = "新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<Integer> addSceneType(@ApiParam(value = "场景类型", required = true) @RequestBody SceneTypeEntity sceneType) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        if(currentUser!=null) {
            sceneType.setCreateBy(currentUser.getName());
            sceneType.setUpdateBy(currentUser.getName());
        }
        int result = sceneTypeService.addSceneType(sceneType) ? 1 : 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }
    /**
     * 批量新增
     */
    @RequestMapping(value = "/batchAdd", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "批量新增或修改")
    public AjaxMessage<Integer> addSceneType(@ApiParam(value = "场景类型", required = true) @RequestBody ArrayList<SceneTypeEntity> sceneTypes) {

        Map<String,String> map = new HashMap<>();
        for (SceneTypeEntity item : sceneTypes) {
            if(map.containsKey(item.getSceneTypeName()))
            {
                return new AjaxMessage<>(ResultStatus.SCENE_TYPE_NAME_ALREADY_EXISTS);
            }else
            {
                map.put(item.getSceneTypeName(),item.getSceneTypeName());
            }
        }

        LoginUser currentUser = UserUtil.getCurrentUser();
        for (SceneTypeEntity item : sceneTypes) {
            item.setTenantId(currentUser.getTenantId());
            item.setStatus(1);
            item.setDateCreate(new Date());
            item.setDateUpdate(new Date());
            item.setCreateBy(currentUser.getName());
            item.setUpdateBy(currentUser.getName());
        }
        int result = sceneTypeService.saveOrUpdateBatch(sceneTypes) ? 1 : 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 修改保存场景类型
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改")
    public AjaxMessage<Integer> editSceneType(@ApiParam(value = "场景类型", required = true) @RequestBody SceneTypeEntity sceneType) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        if(currentUser!=null) {
            sceneType.setUpdateBy(currentUser.getName());
        }
        int result = sceneTypeService.updateSceneTypeById(sceneType) ? 1 : 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<Integer> del(@ApiParam(value = "场景类型ID", required = true) @RequestBody Long[] ids) {
        List<SceneTypeEntity> sceneEntities = sceneTypeService.findByParentSceneTypeIds(ids);
        if (sceneEntities.size() > 0) {
            return new AjaxMessage<>(ResultStatus.SCENETYPE_NAME_ALREADY_EXISTS);
        }
        int result = sceneTypeService.delSceneTypeByIds(ids) ? 1 : 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    @ApiOperation(value = "按ID查询场景类型")
    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    @ResponseBody
    public AjaxMessage<SceneTypeEntity> selectById(@ApiParam(value = "ID", required = true) @RequestParam Long id) {
        SceneTypeEntity sceneTypeEntity = sceneTypeService.findSceneTypeById(id);
        return new AjaxMessage<>(ResultStatus.OK, sceneTypeEntity);
    }
}
