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.service.ZhllCollectService;
import com.dgtis.dorm.manager.db.service.ZhllConsultingComplaintsService;
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.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.HashMap;
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/consulting")
public class WxConsultingController {

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

    @Autowired
    private ZhllConsultingComplaintsService zhllConsultingComplaintsService;

    /**
     * @descption: 新增咨询
     * @param: [zhllConsultingComplaints]
     * @return: java.lang.Object
     * @auther: qxp
     * @date:19-7-19 16:20
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增咨询",notes = "只需填写咨询部门id（deptUnid），咨询事项id（serviceUnid），咨询标题（title）" +
            "咨询内容（content），咨询人id（userId），咨询或投诉类型（type 投诉1 咨询2）")
    public Object save(@RequestBody ZhllConsultingComplaints zhllConsultingComplaints){
        try{
            if (zhllConsultingComplaints.getUserId()!=null&&!zhllConsultingComplaints.getUserId().trim().equals("")){
                if(zhllConsultingComplaints.getServiceUnid()!=null&&!zhllConsultingComplaints.getServiceUnid().trim().equals("")
                        &&zhllConsultingComplaints.getDeptUnid()!=null&&!zhllConsultingComplaints.getDeptUnid().trim().equals("")
                        &&zhllConsultingComplaints.getConsultantName()!=null&&!zhllConsultingComplaints.getConsultantName().trim().equals("")
                        &&zhllConsultingComplaints.getConsultantPhone()!=null&&!zhllConsultingComplaints.getConsultantPhone().trim().equals("")
                        &&zhllConsultingComplaints.getType()!=null&&!zhllConsultingComplaints.getType().trim().equals("")
                ){
                    zhllConsultingComplaints.setId(UUIDUtil.getUUID());
                    zhllConsultingComplaints.setAddUser(zhllConsultingComplaints.getUserId());
                    //确保新增的回答为0
                    zhllConsultingComplaints.setIsAnswer("0");
                    Integer i = zhllConsultingComplaintsService.insertZhllConsultingComplaints(zhllConsultingComplaints);
                    if(i>0){
                        return ResponseUtil.ok();
                    }else{
                        return  ResponseUtil.fail512();//新增失败
                    }
                }else{
                    logger.info("新增咨询参数不全！");
                    return ResponseUtil.fail402();
                }
            }else{
                return ResponseUtil.fail401();//未登录
            }
        }catch(Exception e){
            e.printStackTrace();
            logger.error("新增咨询异常：",e );
            return ResponseUtil.fail();
        }
    }

    /**
     * @descption: 查询我的咨询列表
     * @param: [userId, page, limit]
     * @return: java.lang.Object
     * @auther: qxp
     * @date:19-7-19 16:19
     */
    @RequestMapping("/getListPage")
    @ApiOperation(value = "查询我的咨询列表，分页",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name="rows",value="每页几行（默认10）",required=false, dataType = "Integer"),
            @ApiImplicitParam(name="page",value="第几页（默认1）",required=false, dataType = "Integer"),
            @ApiImplicitParam(name="userId",value="咨询人id",required=true, dataType = "String"),
            @ApiImplicitParam(name="type",value="咨询或投诉 类型",required=false, dataType = "String")
    })
    public Object getListPage(String userId,String type,Integer page,Integer rows ){
        try{
            if(userId!=null&&!userId.trim().equals("") ){
                ZhllConsultingComplaints zhllConsultingComplaints = new ZhllConsultingComplaints();
                zhllConsultingComplaints.setUserId(userId);
                zhllConsultingComplaints.setType(type);
                PageInfo<ZhllConsultingComplaints> pageInfo = zhllConsultingComplaintsService.findListPage(zhllConsultingComplaints,page,rows);
                Map<String,Object> resultMap = new HashMap<>();
                resultMap.put("total",pageInfo.getTotal());
                resultMap.put("items",pageInfo.getList());
                return ResponseUtil.ok(resultMap);
            }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:19
     */
    @RequestMapping("/getInfo")
    @ApiOperation(value = "查询咨询详情",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value="咨询id",required=true, dataType = "String")
    })
    public Object getInfo(String id){
        try{
            if(id!=null&&!id.trim().equals("")){
                ZhllConsultingComplaints zhllConsultingComplaints = zhllConsultingComplaintsService.selectZhllConsultingComplaintsById(id);
                return ResponseUtil.ok(zhllConsultingComplaints);
            }else{
                return ResponseUtil.fail402();
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("查询咨询详情异常：",e );
            return ResponseUtil.fail();
        }

    }

}
