package com.steerinfo.ems.zghbjkbz.controller;

import com.steerinfo.auth.utils.JwtUtil;
import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.ems.zghbjkbz.mapper.ZghbjkbzMapper;
import com.steerinfo.ems.zghbjkbz.model.Zghbjkbz;
import com.steerinfo.ems.zghbjkbz.service.IZghbjkbzService;
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.*;

/**
 * Zghbjkbz RESTful接口：
 * @author generator
 * @version 1.0-SNAPSHORT 2020-01-15 02:38
 * 类描述
 * 修订历史：
 * 日期：2020-01-15
 * 作者：generator
 * 参考：
 * 描述：Zghbjkbz RESTful接口
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@RestController
@RequestMapping("/${api.version}/zghbjkbzs")
public class ZghbjkbzController extends BaseRESTfulController {

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

    @Autowired
    IZghbjkbzService zghbjkbzService;
    
    @Autowired
    private ZghbjkbzMapper zghbjkbzMapper;

    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("zghbjkbz:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        if(parmas.containsKey("wrType") && parmas.get("wrType") != null && !"".equals(parmas.get("wrType").toString())){
            String wrType = parmas.get("wrType").toString();
            if(wrType.indexOf(",") == -1){
                parmas.put("wrType", "and wr_type = '" + wrType + "'");
            } else {
            	wrType = "and wr_type in ('" + wrType.replaceAll(",", "','") + "')";
                parmas.put("wrType", wrType);
            }
        }
        if(parmas.containsKey("location") && parmas.get("location") != null && !"".equals(parmas.get("location").toString())) {
            String locations = parmas.get("location").toString();
            if(locations.indexOf(",") == -1){
                parmas.put("location", "and LOCATION LIKE '%" + locations + "%'");
            }
            else {
                locations = "and (LOCATION LIKE '%" + locations.replaceAll(",", "%' OR LOCATION LIKE '%") + "%')";
                parmas.put("location", locations);
            }
        }
        if(parmas.containsKey("drainName") && parmas.get("drainName") != null && !"".equals(parmas.get("drainName").toString())){
            String drainName = parmas.get("drainName").toString();
            if(drainName.indexOf(",") == -1){
                parmas.put("drainName", "and drain_name LIKE '%" + drainName + "%'");
            } else {
            	drainName = "and (drain_name LIKE '%" + drainName.replaceAll(",", "%' OR drain_name LIKE '%") + "%')";
                parmas.put("drainName", drainName);
            }
        }
        if(parmas.containsKey("checkEle") && parmas.get("checkEle") != null && !"".equals(parmas.get("checkEle").toString())){
            String checkEle = parmas.get("checkEle").toString();
            if(checkEle.indexOf(",") == -1){
                parmas.put("checkEle", "and check_ele = '" + checkEle + "'");
            } else {
            	checkEle = "and check_ele in ('" + checkEle.replaceAll(",", "','") + "')";
                parmas.put("checkEle", checkEle);
            }
        }
        PageList<Zghbjkbz> list = zghbjkbzService.queryForPage(parmas, pageNum, pageSize);
        return success(list);
    }
    
    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("zghbjkbz:view")
    @GetMapping(value = "/list")
    public RESTfulResult listPage(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
    	if(parmas.containsKey("wrType") && parmas.get("wrType") != null && !"".equals(parmas.get("wrType").toString())){
            String wrType = parmas.get("wrType").toString();
            if(wrType.indexOf(",") == -1){
                parmas.put("wrType", "and wr_type = '" + wrType + "'");
            } else {
            	wrType = "and wr_type in ('" + wrType.replaceAll(",", "','") + "')";
                parmas.put("wrType", wrType);
            }
        }
        if(parmas.containsKey("location") && parmas.get("location") != null && !"".equals(parmas.get("location").toString())) {
            String locations = parmas.get("location").toString();
            if(locations.indexOf(",") == -1){
                parmas.put("location", "and LOCATION LIKE '%" + locations + "%'");
            }
            else {
                locations = "and (LOCATION LIKE '%" + locations.replaceAll(",", "%' OR LOCATION LIKE '%") + "%')";
                parmas.put("location", locations);
            }
        }
        if(parmas.containsKey("drainName") && parmas.get("drainName") != null && !"".equals(parmas.get("drainName").toString())){
            String drainName = parmas.get("drainName").toString();
            if(drainName.indexOf(",") == -1){
                parmas.put("drainName", "and drain_name LIKE '%" + drainName + "%'");
            } else {
            	drainName = "and (drain_name LIKE '%" + drainName.replaceAll(",", "%' OR drain_name LIKE '%") + "%')";
                parmas.put("drainName", drainName);
            }
        }
        if(parmas.containsKey("checkEle") && parmas.get("checkEle") != null && !"".equals(parmas.get("checkEle").toString())){
            String checkEle = parmas.get("checkEle").toString();
            if(checkEle.indexOf(",") == -1){
                parmas.put("checkEle", "and check_ele = '" + checkEle + "'");
            } else {
            	checkEle = "and check_ele in ('" + checkEle.replaceAll(",", "','") + "')";
                parmas.put("checkEle", checkEle);
            }
        }
        PageList<Zghbjkbz> list = zghbjkbzService.getDataForPage(parmas, pageNum, pageSize);
        return success(list);
    }

    @ApiOperation(value="创建", notes="根据Zghbjkbz对象创建")
    @ApiImplicitParam(name = "zghbjkbz", value = "详细实体zghbjkbz", required = true, dataType = "Zghbjkbz")
    //@RequiresPermissions("zghbjkbz:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@ModelAttribute Zghbjkbz model){
        model.setUpdateman(JwtUtil.getUseridByToken());
        Zghbjkbz zghbjkbz = zghbjkbzService.add(model);
        return success(zghbjkbz);
    }

    @ApiOperation(value="批量删除", notes="根据传过来的zghbjkbz数组信息来删除")
    @ApiImplicitParam(name = "zghbjkbz", value = "详细实体zghbjkbz", required = true, dataType = "Zghbjkbz")
    //@RequiresPermissions("zghbjkbz:delete")
    @DeleteMapping(value = "/", produces  = "application/json;charset=UTF-8")
    public RESTfulResult delete(@RequestBody Zghbjkbz[] model){
        if(model.length >= 1) {
            int failnum = 0;
            String failmsg = "";
            for (int i = 0; i < model.length; i++) {
            	zghbjkbzService.delete(model[i].getId());
            }
            return success();
        }
        else {
            return failed(null, "请选择删除数据");
        }
    }

    @ApiOperation(value="批量更新详细信息", notes="根据传过来的zghbjkbz数组信息来更新详细信息")
    @ApiImplicitParam(name = "zghbjkbz", value = "详细实体zghbjkbz", required = true, dataType = "Zghbjkbz")
    //@RequiresPermissions("zghbjkbz:update")
    @PutMapping(value = "/", produces  = "application/json;charset=UTF-8")
    public RESTfulResult batchupdate(@RequestBody Zghbjkbz[] model){
        int failnum = 0;
        String failmsg = "";
        List<Zghbjkbz> errorid = new ArrayList<>();
        for(int i = 0;i < model.length;i++){
            try {
                LOGGER.info("用电地点维护修改的第" + (i + 1) + "条数据:");
                LOGGER.info("用电地点维护修改前数据是" + zghbjkbzService.getById(model[i].getId()).toString());
                model[i].setUpdateman(JwtUtil.getUseridByToken());
                Zghbjkbz zghbjkbz = zghbjkbzService.modify(model[i]);
                LOGGER.info("用电地点维护修改后数据是" + zghbjkbz.toString());
            } catch(Exception e){
                failnum++;
                errorid.add(model[i]);
                failmsg += "第" + (i + 1) + "条数据出现错误,错误原因是" + e + ",主键:(id" + model[i].getId() + ")";
            }
        }
        if(failnum > 0){
            LOGGER.info("本次共修改" + model.length + "条数据,其中成功" + (model.length - failnum) + "条,失败" + failnum + "条,失败原因:" + failmsg);
            return success(errorid,"51","本次共修改" + model.length + "条数据,其中成功" + (model.length - failnum) + "条,失败" + failnum + "条");
        }
        return success();
    }
}