package com.steerinfo.ems.tcm0315.controller;

import com.steerinfo.ems.tcm0315.model.TCm0315;
import com.steerinfo.ems.tcm0315.model.VPowerPlanId;
import com.steerinfo.ems.tcm0315.service.ITCm0315Service;
import com.steerinfo.ems.tcm0323.model.TCm0323;
import com.steerinfo.ems.tcm0323.service.ITCm0323Service;
import com.steerinfo.ems.tcm0324.controller.TCm0324Controller;
import com.steerinfo.ems.tcm0325.model.TCm0325;
import com.steerinfo.ems.tcm0325.service.ITCm0325Service;
import com.steerinfo.ems.tcm0348.controller.TCm0348Controller;
import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageList;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * TCm0315 RESTful接口：
 * @author generator
 * @version 1.0-SNAPSHORT 2019-10-28 09:32
 * 类描述
 * 修订历史：
 * 日期：2019-10-28
 * 作者：generator
 * 参考：
 * 描述：TCm0315 RESTful接口
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@RestController
@RequestMapping("/${api.version}/tcm0315s")
public class TCm0315Controller extends BaseRESTfulController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TCm0315Controller.class);

    @Autowired
    ITCm0315Service tCm0315Service;
    @Autowired
    ITCm0325Service tCm0325Service;
    @Autowired
    ITCm0323Service tCm0323Service;
    @Autowired
    TCm0324Controller tCm0324Controller;
    @Autowired
    TCm0348Controller tCm0348Controller;

    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("tcm0315:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<TCm0315> list = tCm0315Service.queryForPage(parmas, pageNum, pageSize);
        return success(list);
    }

    @ApiOperation(value="创建", notes="根据TCm0315对象创建")
    @ApiImplicitParam(name = "tCm0315", value = "详细实体tCm0315", required = true, dataType = "TCm0315")
    //@RequiresPermissions("tcm0315:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@ModelAttribute TCm0315 model) {
    	if(model.getPowersystemname()==null || model.getPowersystemname().isEmpty()){
    		return failed(null, "请选择系统");
    	}
    	if(model.getSitem()==null || model.getSitem().isEmpty() || model.getSitem().split(",").length<1){
    		return failed(null, "请选择项目");
    	}
        TCm0325 update0325 = new TCm0325();
        TCm0323 update0323 = new TCm0323();
        List<String> Sitems = Arrays.asList(model.getSitem().split(","));

        for(int i = 0;i < Sitems.size();i++){
            update0325.setPowercode(model.getPowersystemname());
            update0325.setUsestate("Use");
            tCm0325Service.modify(update0325);

            update0323.setItemcode(Sitems.get(i));
            update0323.setCheckstate("Check");
            TCm0323 tCm0323 = tCm0323Service.modify(update0323);

            model.setSitem(Sitems.get(i));
            model.setUnit(tCm0323.getUnit());
            TCm0315 tCm0315 = tCm0315Service.add(model);

//            tCm0324Controller.add(tCm0315);
//            tCm0348Controller.add(tCm0315);
        }
        return success();
    }

    @ApiOperation(value="更新详细信息", notes="根据url的id来指定更新对象，并根据传过来的tCm0315信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "tCm0315", value = "详细实体tCm0315", required = true, dataType = "TCm0315")
    })
    //@RequiresPermissions("tcm0315:update")
    @PutMapping(value = "/{id}", produces  = "application/json;charset=UTF-8")
    public RESTfulResult update(@PathVariable String id, @RequestBody TCm0315 model){
        model.setRecordid(id);
        TCm0315 before = tCm0315Service.getById(id);
        LOGGER.info("动力监测计划表修改前的数据是" + before.toString());
        TCm0315 tCm0315 = tCm0315Service.modify(model);
        LOGGER.info("动力监测计划表修改后的数据是" + tCm0315.toString());
        tCm0324Controller.update(tCm0315);
        tCm0348Controller.update(before, tCm0315);
        return success(tCm0315);
    }

    @ApiOperation(value="删除", notes="根据url的recordid来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    @DeleteMapping(value = "/{id}")
    public RESTfulResult delete(@PathVariable String id){
        Map<String, Object> parameters = new HashMap();
        Map<String, Object> parameters2 = new HashMap();
        String[] idarray = id.split(",");
        List<String> Ids = new ArrayList<>();
        for(int i = 0;i < idarray.length;i++) {
            Ids.add(idarray[i]);
        }
        for(int i = 0;i < Ids.size();i++){
            TCm0315 model = tCm0315Service.getById(Ids.get(i));
            LOGGER.info("动力监测计划表要删除的数据是" + model.toString());
            parameters.put("powercode",model.getPowersystemname());
            parameters2.put("itemcode",model.getSitem());

            tCm0348Controller.delete(model);
            tCm0324Controller.delete(Ids.get(i));
            tCm0315Service.delete(Ids.get(i));

            if (tCm0325Service.getusestate(parameters) == null) {
                TCm0325 tCm0325 = new TCm0325();
                tCm0325.setPowercode(model.getPowersystemname());
                tCm0325.setUsestate("NoUse");
                tCm0325Service.modify(tCm0325);
            }
            if (tCm0323Service.getcheckstate(parameters2) == null) {
                TCm0323 tCm0323 = new TCm0323();
                tCm0323.setItemcode(model.getSitem());
                tCm0323.setCheckstate("NoCheck");
                tCm0323Service.modify(tCm0323);
            }
        }
        LOGGER.info("动力监测计划表共删除数据" + Ids.size() + "条。" );
        return success();
    }

    @ApiOperation(value="水质监测数据录入的检测计划下拉框", notes="查询id和name")
    @GetMapping(value = "/getplanidandname/")
    public RESTfulResult getPlanNameAndId(){
        List<VPowerPlanId> vPowerPlanId = tCm0315Service.getPlanNameAndId();
        System.out.println(vPowerPlanId);
        return success(vPowerPlanId);
    }
}
