package com.zcxk.controller;

import com.zcxk.dto.ThingDetailInfoDto;
import com.zcxk.dto.ThingInfoDto;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.ResultStatus;
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.ThingSettingEntity;
import com.zcxk.service.ThingSettingService;

/**
 * 物品设置页面控制器
 * @author WYY
 * @date 2020-12-28 15:18
 */
@Controller
@RequestMapping("/thingsetting")
@Api(tags = "物品设置")
public class ThingSettingController{
    @Autowired
    private ThingSettingService thingSettingService;

        
    @RequestMapping(value = "/findList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询物品统计信息")
    public AjaxMessage<List<ThingSettingEntity>> findList(@ApiParam(value = "物品类型(1管网 2窨井 3阀门 4消防栓)", required = true) @RequestParam Integer thingType) {
        ThingSettingEntity thingSettingEntity = new ThingSettingEntity();
        thingSettingEntity.setThingType(thingType);
        List<ThingSettingEntity> thingSettingEntities = thingSettingService.findList(thingSettingEntity);
        return new AjaxMessage<>(ResultStatus.OK, thingSettingEntities);
    }
    @RequestMapping(value = "/findDetailList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询物品统计信息")
    public AjaxMessage<List<ThingInfoDto>> findDetailList(@ApiParam(value = "物品类型(1管网 2窨井 3阀门 4消防栓)", required = true) @RequestParam Integer thingType) {
        ThingSettingEntity thingSettingEntity = new ThingSettingEntity();
        thingSettingEntity.setThingType(thingType);
        List<ThingSettingEntity> thingSettingEntities = thingSettingService.findList(thingSettingEntity);
        List<ThingInfoDto> thingInfoDtos = new ArrayList<>();
        Map<Integer,List<ThingDetailInfoDto>> map = new HashMap<>();
        for(ThingSettingEntity item:thingSettingEntities)
        {
            List<ThingDetailInfoDto> list = new ArrayList<>();
           Integer key = item.getThingObjectType();
           if(!map.containsKey(key.intValue())) {
               ThingDetailInfoDto detailInfoDto = new ThingDetailInfoDto();
               detailInfoDto.setThingBore(item.getThingBore());
               detailInfoDto.setThingCount(item.getThingCount());
               detailInfoDto.setThingLength(item.getThingLength());
               list.add(detailInfoDto);
               map.put(key, list);
           }else {
               ThingDetailInfoDto detailInfoDto = new ThingDetailInfoDto();
               detailInfoDto.setThingBore(item.getThingBore());
               detailInfoDto.setThingCount(item.getThingCount());
               detailInfoDto.setThingLength(item.getThingLength());
               map.get(key.intValue()).add(detailInfoDto);
           }
        }
        for (Integer key : map.keySet()) {
            ThingInfoDto thingInfoDto = new ThingInfoDto();
            thingInfoDto.setThingObjectType(key);
            thingInfoDto.setDetailInfoDtoList(map.get(key.intValue()));
            thingInfoDtos.add(thingInfoDto) ;
        }
        return new AjaxMessage<>(ResultStatus.OK, thingInfoDtos);
    }

    @RequestMapping(value = "/findTotalList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询物品统计汇总信息")
    public AjaxMessage<List<ThingSettingEntity>> findTotalList(@ApiParam(value = "物品类型(1管网 2窨井 3阀门 4消防栓)", required = false) @RequestParam(required = false) Integer thingType) {
        ThingSettingEntity thingSettingEntity = new ThingSettingEntity();
        thingSettingEntity.setThingType(thingType);
        List<ThingSettingEntity> thingSettingEntities = thingSettingService.findTotalList(thingSettingEntity);
        return new AjaxMessage<>(ResultStatus.OK, thingSettingEntities);
    }
    /**
     * 新增
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public  AjaxMessage<Integer> addThingSetting(@ApiParam(value = "物品统计设置", required = true) @RequestBody ThingSettingEntity thingSetting) {
        //校验参数
        int result = thingSettingService.addThingSetting(thingSetting) ? 1 : 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }
    

    
    /**
     * 修改
     */
    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    public AjaxMessage<ThingSettingEntity> selectById(@ApiParam(value = "id", required = true) @RequestParam Long id){
        ThingSettingEntity thingSetting = thingSettingService.findThingSettingById(id);
        return  new AjaxMessage<>(ResultStatus.OK, thingSetting);
    }

    /**
     * 修改保存物品设置
     */
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<Integer> editThingSetting(@ApiParam(value = "物品统计设置", required = true) @RequestBody ThingSettingEntity thingSetting) {
        int result = thingSettingService.updateThingSettingById(thingSetting) ? 1 : 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<Integer>  del(@ApiParam(value = "ids", required = true) @RequestBody Long[] ids) {
        int result =  thingSettingService.delThingSettingByIds(ids)? 1: 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }
}
