package org.gyk.modules.sz.jldxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.gyk.common.annotation.AutoLog;
import org.gyk.common.api.Result;
import org.gyk.common.base.controller.gykController;
import org.gyk.modules.sz.jldxx.entity.CxcSssjSzJldxx;
import org.gyk.modules.sz.jldxx.mapper.CxcSssjSzJldxxMapper;
import org.gyk.modules.sz.jldxx.service.ICxcSssjSzJldxxService;
import org.gyk.modules.sz.jldxx.service.IGetJldxxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 计量点信息设置
 * @Author: jeecg-boot
 * @Date:   2021-03-09
 * @Version: V1.0
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(tags="cxc_sssj_sz_jldxx")
@RestController
@RequestMapping("/jldxx/cxcSssjSzJldxx")
@Slf4j
public class CxcSssjSzJldxxController extends gykController<CxcSssjSzJldxx, ICxcSssjSzJldxxService> {
	@Autowired
	private ICxcSssjSzJldxxService cxcSssjSzJldxxService;
	@Autowired
	private IGetJldxxService getAllJldxx;
	@Autowired (required=false)
	private CxcSssjSzJldxxMapper cxcSssjSzJldxxMapper;

	/**
	 *   添加
	 *
	 * @param cxcSssjSzJldxx
	 * @return
	 */
	@AutoLog(value = "cxc_sssj_sz_jldxx-添加")
	@ApiOperation(value="cxc_sssj_sz_jldxx-添加", notes="cxc_sssj_sz_jldxx-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody CxcSssjSzJldxx cxcSssjSzJldxx) {
		cxcSssjSzJldxxService.save(cxcSssjSzJldxx);
		return Result.ok("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param cxcSssjSzJldxx
	 * @return
	 */
	@AutoLog(value = "cxc_sssj_sz_jldxx-编辑")
	@ApiOperation(value="cxc_sssj_sz_jldxx-编辑", notes="cxc_sssj_sz_jldxx-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody CxcSssjSzJldxx cxcSssjSzJldxx) {
		cxcSssjSzJldxxService.updateById(cxcSssjSzJldxx);
		return Result.ok("保存成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "cxc_sssj_sz_jldxx-通过id删除")
	@ApiOperation(value="cxc_sssj_sz_jldxx-通过id删除", notes="cxc_sssj_sz_jldxx-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		cxcSssjSzJldxxService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "cxc_sssj_sz_jldxx-批量删除")
	@ApiOperation(value="cxc_sssj_sz_jldxx-批量删除", notes="cxc_sssj_sz_jldxx-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.cxcSssjSzJldxxService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "cxc_sssj_sz_jldxx-通过id查询")
	@ApiOperation(value="cxc_sssj_sz_jldxx-通过id查询", notes="cxc_sssj_sz_jldxx-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		CxcSssjSzJldxx cxcSssjSzJldxx = cxcSssjSzJldxxService.getById(id);
		if(cxcSssjSzJldxx==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(cxcSssjSzJldxx);
	}

	@GetMapping(value = "/listAllJldxx")
	public Result<?> listAllJldxx() {
//		LambdaQueryWrapper<CxcSssjSzJldxx> qw = Wrappers.lambdaQuery();
//		qw.orderByAsc(CxcSssjSzJldxx::getStationName,CxcSssjSzJldxx::getJldName);
		QueryWrapper<CxcSssjSzJldxx> qw = new QueryWrapper<>();
		qw.orderByAsc("station_name","jld_name");
		List<CxcSssjSzJldxx> list = cxcSssjSzJldxxMapper.selectList(qw);
		for (CxcSssjSzJldxx cxcSssjSzJldxx : list) {
			if (cxcSssjSzJldxx.getStationName().equals("柳屯配气站B") || cxcSssjSzJldxx.getStationName().equals("柳屯配气站A")) {
				cxcSssjSzJldxx.setStationName("柳屯配气站");
			}
		}

		if(list.size()<1) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(list);
	}

	@GetMapping(value = "/getjldxxByZcAndJldname")
	public Result<?> getjldxxByZcAndJldname(@RequestParam(name="zcmc",required=true) String zcmc,@RequestParam(name="jldmc",required=true) String jldmc) {

		LambdaQueryWrapper<CxcSssjSzJldxx> qw = Wrappers.lambdaQuery();
		qw.eq(CxcSssjSzJldxx::getStationName,zcmc);
		qw.eq(CxcSssjSzJldxx::getJldName,"jl"+jldmc);
		List<CxcSssjSzJldxx> list = cxcSssjSzJldxxMapper.selectList(qw);
		List<String> list1 = new ArrayList<>();
		if (list.size()>0) {
			//根据sssjtools第658行部分，3,4,10分别对应压力，压差和温度
			String[] gykbz= list.get(0).getGykbz().split("\\,");
			list1.add("yl:"+gykbz[3]);
			list1.add("yc:"+gykbz[4]);
			list1.add("wd:"+gykbz[10]);
		}
		
		return Result.ok(list1);
	}


}
