package com.unis.base.server.controller;

import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.CompanyRecordsQueryVO;
import com.unis.base.common.vo.CompanyRecordsVO;
import com.unis.base.server.service.CompanyRecordsService;
import com.unis.common.utils.ResponseWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author WangZL
 * @version 1.0
 * @Title CompanyRecordsController
 * @ProjectName 企业记录 Controller
 * @Description
 * @date 2019/07/22 17点26分
 */
@RestController
@Api(value = "企业记录", tags = "企业记录")
@Slf4j
public class CompanyRecordsController {

	@Autowired
	private CompanyRecordsService companyRecordsService;

	/**
	 * @author WangZL
	 * @Description 获取设备列表
	 * @param 涉粮企业备案记录ID，统一社会信用代码，备案时间（年度），是否备案
	 * @return 企业记录列表
	 */
	@ApiOperation(value = "企业记录-查询列表", notes = "参数：涉粮企业备案记录ID，统一社会信用代码，备案时间（年度），是否备案")
	@PostMapping(value = "/companyRecordsList")
    public ResponseWrapper getCompanyRecordsList(@RequestBody CompanyRecordsQueryVO companyRecordsQueryVO){
        try {
        	List<CompanyRecordsVO> CompanyRecordsList = companyRecordsService.getCompanyRecordsList(companyRecordsQueryVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), CompanyRecordsList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

	/**
	 * @author WangZL
	 * @Description 获取企业记录列表
	 * @param 涉粮企业备案记录ID，统一社会信用代码，备案时间（年度），是否备案
	 * @return 企业记录列表
	 */
	@ApiOperation(value = "企业记录-分页查询列表", notes = "参数：涉粮企业备案记录ID，统一社会信用代码，备案时间（年度），是否备案")
	@PostMapping(value = "/companyRecordsPage/{pageNum}/{pageSize}")
	public ResponseWrapper getCompanyRecordsPage(@RequestBody CompanyRecordsQueryVO companyRecordsQueryVO,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try {
			PageInfo<CompanyRecordsVO> pageInfos = new PageInfo<CompanyRecordsVO>(companyRecordsService.getCompanyRecordsPage(companyRecordsQueryVO,pageNum,pageSize)) ;
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pageInfos);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

	/**
	 * @author WangZL
	 * @Description 根据设备编码获取企业记录
	 * @return
	 */
	@ApiOperation(value = "企业记录-查询详情", notes = "参数：企业记录ID")
	@GetMapping(value = "/companyRecords/{qybabh}")
	public ResponseWrapper getCompanyRecordsInfoById(@PathVariable("qybabh")String qybabh) {
		try {
			CompanyRecordsVO companyRecordsVO = companyRecordsService.getCompanyRecordsInfoById(qybabh);
			if(companyRecordsVO == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "获取失败，该企业记录不存在", null);
			}
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), companyRecordsVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}


	/**
	 * @author WangZL
	 * @Description 新增企业记录
	 * @param 企业记录信息
	 * @return
	 */
	@PostMapping(value = "/companyRecords")
	@ApiOperation(value = "企业记录-根据备案状态新增", notes = "参数：企业记录信息")
    public ResponseWrapper insert(@RequestBody CompanyRecordsVO companyRecordsVO) {
		try{
			if(companyRecordsVO.getSfba() == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			String qybabh = companyRecordsService.insert(companyRecordsVO);
			if (qybabh == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "添加失败", null);
			}
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),qybabh);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

	/**
	 * @author WangZL
	 * @Description 更新
	 * @param 企业记录信息 models
	 * @return
	 */
	@PutMapping(value = "/companyRecords")
	@ApiOperation(value = "企业记录-更新", notes = "参数：企业记录信息")
	public ResponseWrapper update(@RequestBody CompanyRecordsVO companyRecordsVO) {
		try{
			int x = companyRecordsService.update(companyRecordsVO);
			if(x>0) {
				return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),null);
			} else if (x==0) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "更新失败", null);
			} else {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "更新失败，无此库点信息", null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}
}
