package com.dgtis.dorm.manager.wx.web;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.dgtis.dorm.manager.db.domain.ZhllConsultingComplaints;
import com.dgtis.dorm.manager.db.domain.ZhllUserMaterial;
import com.dgtis.dorm.manager.db.service.ZhllConsultingComplaintsService;
import com.dgtis.dorm.manager.db.service.ZhllUserMaterialService;
import com.dgtis.dorm.manager.db.util.ResponseUtil;
import com.dgtis.dorm.manager.wx.util.UUIDUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qxp
 * @version 1.0
 * @descption: 微信小程序我的材料相关的API
 * @company 神州数码通用软件（洛阳）有限公司
 * @copyright (c) 2019 LuoYang DGT Co'Ltd Inc. All rights reserved.
 * @date 19-7-10 17:05
 * @since JDK1.8
 */
@Api(tags = "微信小程序我的材料相关的API")
@RestController
@RequestMapping("/wx/material")
public class WxMaterialController {

    private final Log logger = LogFactory.getLog(WxAuthController.class);

    @Value("${resourcePrefix}")
    private String resourcePrefix;

    @Autowired
    private ZhllUserMaterialService zhllUserMaterialService;

    /**
     * @descption: 新增材料
     * @param: [zhllUserMaterial]
     * @return: java.lang.Object
     * @auther: qxp
     * @date:19-7-19 16:21
     */
    @RequestMapping("/save")
    @ApiOperation(value = "新增材料",notes = "需填写材料名称（materialName），材料路径（materialUrl）" +
            "用户id（userId）",httpMethod = "POST")
    public Object save(@RequestBody ZhllUserMaterial zhllUserMaterial){
        try{
            if(zhllUserMaterial.getUserId()!=null&&!zhllUserMaterial.getUserId().trim().equals("")){
                if(zhllUserMaterial.getMaterialName()!=null&&!zhllUserMaterial.getMaterialName().trim().equals("")
                        &&zhllUserMaterial.getMaterialUrl()!=null&&!zhllUserMaterial.getMaterialUrl().trim().equals("")
                ){
                    zhllUserMaterial.setId(UUIDUtil.getUUID());
                    zhllUserMaterial.setAddUser(zhllUserMaterial.getUserId());
                    Integer i = zhllUserMaterialService.insertZhllUserMaterial(zhllUserMaterial);
                    if(i>0){
                        return ResponseUtil.ok();
                    }else{
                        return  ResponseUtil.fail512();
                    }
                }else{
                    return ResponseUtil.fail402();
                }
            }else{
                return ResponseUtil.fail401();
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("新增材料异常:",e);
            return ResponseUtil.fail();
        }
    }

    /**
     * @descption: 查询我的材料列表
     * @param: [userId]
     * @return: java.lang.Object
     * @auther: qxp
     * @date:19-7-19 16:21
     */
    @RequestMapping("/getList")
    @ApiOperation(value = "查询我的材料列表",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value="用户id",required=true, dataType = "String")
    })
    public Object getList(String userId){
        try{
            if(userId!=null&&!userId.trim().equals("")
            ){
                ZhllUserMaterial zhllUserMaterial = new ZhllUserMaterial();
                zhllUserMaterial.setUserId(userId);
                List<ZhllUserMaterial> resultList = zhllUserMaterialService.selectZhllUserMaterialList(zhllUserMaterial);
                Map<String,Object>result= new HashMap<>();
                result.put("items",resultList);
                result.put("prefix",resourcePrefix);
                return ResponseUtil.ok(result);
            }else{
                return ResponseUtil.fail401();
            }
        }catch(Exception e){
            e.printStackTrace();
            logger.error("查询我的材料列表异常:",e);
            return ResponseUtil.fail();
        }

    }

    /**
     * @descption: 删除我的材料
     * @param: [id]
     * @return: java.lang.Object
     * @auther: qxp
     * @date:19-7-19 16:21
     */
    @RequestMapping("/delete")
    @ApiOperation(value = "删除我的材料",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value="材料id",required=true, dataType = "String")
    })
    public Object delete(String id){
        try{
            if(id!=null&&!id.trim().equals("")){
                Integer i = zhllUserMaterialService.deleteById(id);
                if(i>0){
                    return ResponseUtil.ok();
                }else{
                    return ResponseUtil.fail511();//删除失败
                }
            }else{
                logger.info("查询咨询详情参数不全！");
                return ResponseUtil.fail402();
            }
        }catch (Exception e ){
            e.printStackTrace();
            logger.error("删除我的材料:",e);
            return ResponseUtil.fail();
        }

    }

}
