/*
 * ShouGouZiGeController.java
 * 版权所有 © 2011-2017 北京神舟良实信息技术有限公司
 * Copyright © 2011-2017 All Rights Reserved    京ICP备13013562号
 *  created by lipenghe with LAPTOP-DPSC399A
 */
package com.unis.base.server.controller;

import com.unis.base.common.vo.*;
import com.unis.base.server.service.ShouGouZiGeService;
import com.unis.common.constant.BaseStatusConstant;
import com.unis.common.utils.*;
import com.unis.common.vo.groups.InsertGroup;
import com.unis.common.vo.groups.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * TY_SHOUGOUZIGE-真收购资格表
 * @author lipenghe at 
 */
@RestController
@RequestMapping("")
@Api( tags = "真收购资格表---真收购资格表")
public class ShouGouZiGeController {

    @Autowired
    private ShouGouZiGeService shouGouZiGeService;

    /*@ApiOperation(value = "真收购资格表---真收购资格表---选择性保存,包含新增和修改", notes = "真收购资格表")
    @PostMapping("/shouGouZiGe/saveEntitySelective")
    public ResponseWrapper saveEntitySelective(
        @RequestBody ShouGouZiGeVO ziGeVO) {
        return shouGouZiGeService.saveEntitySelectiveResult(ziGeVO);
    }*/

    @ApiOperation(value = "真收购资格表---真收购资格表---选择性插入字段", notes = "真收购资格表")
    @PostMapping("/shouGouZiGe/insertEntitySelective")
    public ResponseWrapper insertEntitySelective(
        @Validated({InsertGroup.class}) @RequestBody ShouGouZiGeVO ziGeVO
        , BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            return ResponseWrapper.validateFail(bindingResult.getAllErrors());
        }else{
            if(ziGeVO.getYxzt()==null){
                ziGeVO.setYxzt(1);
            }
            return shouGouZiGeService.insertEntitySelectiveResult(ziGeVO);
        }
    }

    @ApiOperation(value = "真收购资格表---真收购资格表---批量插入全部字段", notes = "真收购资格表")
    @PostMapping("/shouGouZiGe/insertBatchEntityList")
    public ResponseWrapper insertBatchEntityList(
        @RequestBody List<ShouGouZiGeVO> ziGeVOList) {
        return shouGouZiGeService.insertBatchEntityListResult(ziGeVOList);
    }

    @ApiOperation(value = "真收购资格表---真收购资格表---根据实体不为空的属性,选择性更新字段", notes = "真收购资格表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sgzgid", value = "收购资格id#<主键><唯一><检索><非空>", dataType = "Long", paramType = "header")
    })
    @PostMapping("/shouGouZiGe/updateByEntitySelective")
    public ResponseWrapper updateByEntitySelective(
        @Validated({UpdateGroup.class}) @RequestBody ShouGouZiGeVO ziGeVO
        , BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            return ResponseWrapper.validateFail(bindingResult.getAllErrors());
        }else{
            return shouGouZiGeService.updateByEntitySelectiveResult(ziGeVO, null);
        }
    }

    @ApiOperation(value = "真收购资格表---真收购资格表---更改状态", notes = "真收购资格表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sgzgid", value = "收购资格id#<主键><唯一><检索><非空>", dataType = "Long", paramType = "header")
    })
    @PostMapping("/shouGouZiGe/updateStatusByEntity")
    public ResponseWrapper updateStatusByEntity(
        @RequestBody ShouGouZiGeVO ziGeVO) {
        if(ziGeVO!= null){
            return shouGouZiGeService.updateStatusByEntityResult(ziGeVO, null);
        }else{
            return ResponseWrapper.invalid();
        }
    }

    @ApiOperation(value = "真收购资格表---真收购资格表---[真删除]根据实体的某些属性进行单条或者批量删除", notes = "真收购资格表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sgzgid", value = "收购资格id#<主键><唯一><检索><非空>", dataType = "Long", paramType = "header")
    })
    @PostMapping("/shouGouZiGe/deleteByEntity")
    public ResponseWrapper deleteByEntity(
        @RequestBody ShouGouZiGeShowVO ziGeShowVO){
        if(ziGeShowVO!= null){
            ShouGouZiGeVO ziGeVO= JsonUtils.parse(
                JsonUtils.serialize(ziGeShowVO), ShouGouZiGeVO.class);
            ziGeVO.setStatus(Integer.valueOf(BaseStatusConstant.STATUS_DELETED));
            return shouGouZiGeService.deleteByEntityResult(ziGeVO, null);
        }else{
            return ResponseWrapper.invalid();
        }
    }

    @ApiOperation(value = "真收购资格表---真收购资格表---根据实体(唯一标示)查询一条数据", notes = "真收购资格表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sgzgid", value = "收购资格id#<主键><唯一><检索><非空>", dataType = "Long", paramType = "header")
    })
    @PostMapping("/shouGouZiGe/selectById")
    public ResponseWrapper selectById(
        @RequestBody ShouGouZiGeShowVO ziGeShowVO){
        if(ziGeShowVO!= null){
            ShouGouZiGeVO ziGeVO= JsonUtils.parse(
                JsonUtils.serialize(ziGeShowVO), ShouGouZiGeVO.class);
            return shouGouZiGeService.selectByIdResult(ziGeVO);
        }else{
            return ResponseWrapper.invalid();
        }
    }

    @ApiOperation(value = "真收购资格表---真收购资格表---根据实体的某些属性查询分页数据, pageSize, pageNum从url传", notes = "真收购资格表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageSize", value = "分页大小", dataType = "int", paramType = "query")
        ,@ApiImplicitParam(name = "pageNum", value = "当前页", dataType = "int", paramType = "query")
        ,@ApiImplicitParam(name = "sgzgid", value = "收购资格id#<主键><唯一><检索><非空>", dataType = "Long", paramType = "header")
        ,@ApiImplicitParam(name = "yyzzzch", value = "营业执照注册号#<检索>", dataType = "String", paramType = "header")
        ,@ApiImplicitParam(name = "tyshxydm", value = "统一社会信用代码#<检索><非空>", dataType = "String", paramType = "header")
        ,@ApiImplicitParam(name = "scztmc", value = "市场主体名称#<检索><非空>", dataType = "String", paramType = "header")
        ,@ApiImplicitParam(name = "zzjgdm", value = "组织机构代码#<检索>", dataType = "String", paramType = "header")
        ,@ApiImplicitParam(name = "xkjgbm", value = "许可机关编码#<检索>", dataType = "String", paramType = "header")
        ,@ApiImplicitParam(name = "xkjgbmmc", value = "许可机关编码名称#<检索>", dataType = "String", paramType = "header")
        ,@ApiImplicitParam(name = "yxzt", value = "有效状态#<检索>1有效0无效", dataType = "int", paramType = "header")
        ,@ApiImplicitParam(name = "yxqq", value = "有效期起#<检索>", dataType = "Long", paramType = "header")
        ,@ApiImplicitParam(name = "yxqz", value = "有效期止#<检索>", dataType = "Long", paramType = "header")
    })
    @PostMapping("/shouGouZiGe/selectPageByEntity")
    public ResponseWrapper selectPageByEntity(
        @RequestParam(value = "pageNum", defaultValue = "1") int pageNum
        ,@RequestParam(value = "pageSize", defaultValue = "10") int pageSize
        ,@RequestBody ShouGouZiGeListVO ziGeListVO) {
        ShouGouZiGeVO ziGeVO= JsonUtils.parse(
            JsonUtils.serialize(ziGeListVO), ShouGouZiGeVO.class);
        return shouGouZiGeService.selectPageByEntityResult(pageNum, pageSize, ziGeVO, "SGZGID DESC");
    }

    @ApiOperation(value = "真收购资格表---真收购资格表---根据实体的某些属性查询列表", notes = "真收购资格表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sgzgid", value = "收购资格id#<主键><唯一><检索><非空>", dataType = "Long", paramType = "header")
        ,@ApiImplicitParam(name = "yyzzzch", value = "营业执照注册号#<检索>", dataType = "String", paramType = "header")
        ,@ApiImplicitParam(name = "tyshxydm", value = "统一社会信用代码#<检索><非空>", dataType = "String", paramType = "header")
        ,@ApiImplicitParam(name = "scztmc", value = "市场主体名称#<检索><非空>", dataType = "String", paramType = "header")
        ,@ApiImplicitParam(name = "zzjgdm", value = "组织机构代码#<检索>", dataType = "String", paramType = "header")
        ,@ApiImplicitParam(name = "xkjgbm", value = "许可机关编码#<检索>", dataType = "String", paramType = "header")
        ,@ApiImplicitParam(name = "xkjgbmmc", value = "许可机关编码名称#<检索>", dataType = "String", paramType = "header")
        ,@ApiImplicitParam(name = "yxzt", value = "有效状态#<检索>1有效0无效", dataType = "int", paramType = "header")
        ,@ApiImplicitParam(name = "yxqq", value = "有效期起#<检索>", dataType = "Long", paramType = "header")
        ,@ApiImplicitParam(name = "yxqz", value = "有效期止#<检索>", dataType = "Long", paramType = "header")
    })
    @PostMapping("/shouGouZiGe/selectByEntity")
    public ResponseWrapper selectByEntity(
        @RequestBody ShouGouZiGeListVO ziGeListVO) {
        ShouGouZiGeVO ziGeVO= JsonUtils.parse(
            JsonUtils.serialize(ziGeListVO), ShouGouZiGeVO.class);
        return shouGouZiGeService.selectByEntityResult(ziGeVO);
    }

    @ApiOperation(value = "真收购资格表---真收购资格表---根据条件导出数据", notes = "真收购资格表---")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fields", value = "下载的字段:,分隔属性名-分隔指定中文名,如name-姓名, age-年龄", dataType = "String", paramType = "header")
            ,@ApiImplicitParam(name = "sgzgid", value = "收购资格id#<主键><唯一><检索><非空>", dataType = "Long", paramType = "header")
            ,@ApiImplicitParam(name = "yyzzzch", value = "营业执照注册号#<检索>", dataType = "String", paramType = "header")
            ,@ApiImplicitParam(name = "tyshxydm", value = "统一社会信用代码#<检索><非空>", dataType = "String", paramType = "header")
            ,@ApiImplicitParam(name = "scztmc", value = "市场主体名称#<检索><非空>", dataType = "String", paramType = "header")
            ,@ApiImplicitParam(name = "zzjgdm", value = "组织机构代码#<检索>", dataType = "String", paramType = "header")
            ,@ApiImplicitParam(name = "xkjgbm", value = "许可机关编码#<检索>", dataType = "String", paramType = "header")
            ,@ApiImplicitParam(name = "xkjgbmmc", value = "许可机关编码名称#<检索>", dataType = "String", paramType = "header")
            ,@ApiImplicitParam(name = "yxzt", value = "有效状态#<检索>1有效0无效", dataType = "int", paramType = "header")
            ,@ApiImplicitParam(name = "yxqq", value = "有效期起#<检索>", dataType = "Long", paramType = "header")
            ,@ApiImplicitParam(name = "yxqz", value = "有效期止#<检索>", dataType = "Long", paramType = "header")
    })
    @RequestMapping("/shouGouZiGe/exportByEntity")
    public void exportByEntity(HttpServletRequest request, HttpServletResponse response
            , @ModelAttribute ShouGouZiGeListVO ziGeListVO
            , @RequestParam(value= "fields", required = false) String fields) throws IOException {
        ShouGouZiGeVO ziGeVO= JsonUtils.parse(
                JsonUtils.serialize(ziGeListVO), ShouGouZiGeVO.class);
        List<ShouGouZiGeVO> ziGeVOList= shouGouZiGeService.exportByEntityResult(ziGeVO);
        List dataList = JsonUtils.parseList(
                JsonUtils.serialize(ziGeVOList), Map.class);
        //fileName导出文件名
        String fileName= "真收购资格表---导出.xls";
        //sheetName表格名
        String sheetName= "真收购资格表---数据";
        //headMap表头Map, key为属性名, value为展示名,根据实际情况修改
        LinkedHashMap<String, String> headMap= new LinkedHashMap();
        FieldUtil.getPoweredOrderedFields(headMap, new ShouGouZiGeVO(), new BaseEntity());
        if(StringUtils.isNotEmpty(fields)){
            FieldUtil.resloveImportFields(fields, headMap);
        }
        JxlModelExcelUtil.exportExcel(request, response, fileName, dataList, sheetName, headMap);
    }

    @ApiOperation(value = "真收购资格表---真收购资格表---根据条件导入数据   multipart/form-data", notes = "真收购资格表---")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件对象集合,请求必须设置multipart/form-data", dataType = "String", paramType = "header")
            ,@ApiImplicitParam(name = "model", value = "user对象,可无如有此属性contentTpe= application/json", dataType = "String", paramType = "header")
            ,@ApiImplicitParam(name = "config", value = "config对象,上传设置,可无如有此属性contentTpe= application/json", dataType = "String", paramType = "header")
    })
    @PostMapping(value= "/shouGouZiGe/importByEntity", headers= "content-type= multipart/form-data", consumes= "multipart/*")
    public ResponseWrapper importByEntity(HttpServletRequest request, HttpServletResponse response
            ,@RequestParam(value= "file") List<MultipartFile> fileList
            ,@RequestPart(value = "model", required = false) ShouGouZiGeShowVO ziGeShowVO
            ,@RequestPart(value = "config", required = false) ImportConfigVO importConfigVO) throws IOException {
        ShouGouZiGeVO ziGeVO= JsonUtils.parse(
                JsonUtils.serialize(ziGeShowVO), ShouGouZiGeVO.class);
        if(fileList== null || fileList.size()== 0){
            return ResponseWrapper.fail("无文件数据");
        } else{
            return shouGouZiGeService.importByEntityResult(ziGeVO, fileList, importConfigVO);
        }
    }

    @ApiOperation(value = "真收购资格表---真收购资格表---初始化数据", notes = "真收购资格表")
    @PostMapping("/shouGouZiGe/selectInitData")
    public ResponseWrapper selectInitData(
        @RequestBody ShouGouZiGeVO ziGeVO){
        if(ziGeVO!= null){
            return shouGouZiGeService.selectInitDataResult(ziGeVO);
        }else{
            return ResponseWrapper.invalid();
        }
    }

}
