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.ZhllCollect;
import com.dgtis.dorm.manager.db.service.ZhllCollectService;
import com.dgtis.dorm.manager.db.util.JacksonUtil;
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.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 13:54
 * @since JDK1.8
 */
@Api(tags = "微信小程序收藏相关API")
@RestController
@RequestMapping("/wx/collect")
public class WxCollectController {
    private final Log logger = LogFactory.getLog(WxAuthController.class);

    @Autowired
    private ZhllCollectService zhllCollectService;

    /**
     * @descption: 新增收藏
     * @param: [zhllCollect]
     * @return: java.lang.Object
     * @auther: qxp
     * @date:19-7-19 16:18
     */
    @PostMapping(value = "/save")
    @ApiOperation(value = "新增收藏事项")
    public Object save(@RequestBody ZhllCollect zhllCollect ){
       try{
           if(zhllCollect.getUserId()!=null&&!zhllCollect.getUserId().trim().equals("")){
               if(zhllCollect.getType()!=null&&!zhllCollect.getType().trim().equals("")
                       &&zhllCollect.getServiceUnid()!=null&&!zhllCollect.getServiceUnid().trim().equals("")
                       &&zhllCollect.getServiceName()!=null&&!zhllCollect.getServiceName().trim().equals("")
               ){

                   zhllCollect.setId(UUIDUtil.getUUID());
                   zhllCollect.setAddUser(zhllCollect.getUserId());
                   Integer i = zhllCollectService.insertZhllCollect(zhllCollect);
                   if(i>0){
                       logger.info("新增收藏事项成功！");
                       return ResponseUtil.ok();
                   }else{
                       return  ResponseUtil.fail512();
                   }
               }else{
                   logger.info("新增收藏事项缺少参数！");
                   return ResponseUtil.fail402();
               }
           }else{
               logger.info("未登录");
               return ResponseUtil.fail401();
           }
       }catch(Exception e){
           e.printStackTrace();
           logger.error("新增收藏事项异常:",e);
           return ResponseUtil.fail();
       }


    }

    /**
     * @descption: 查询用户收藏的事项
     * @param: [userId, type]
     * @return: java.lang.Object
     * @auther: qxp
     * @date:19-7-19 16:18
     */
    @RequestMapping("/getListByUserId")
    @ApiOperation(value = "根据用户id 查询收藏的事项",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value="用户id",required=true, dataType = "String"),
            @ApiImplicitParam(name="type",value="收藏类型（0.全部1.事项、2.预约）",required=false, dataType = "String"),
    })
    public Object getListByUserId(String userId,String type,Integer page,Integer rows){
        try{
            if(userId!=null&&!userId.trim().equals("")){
                ZhllCollect  zhllCollect = new ZhllCollect();
                zhllCollect.setUserId(userId);
                if(type!=null){
                    if(!type.equals("0")){
                        zhllCollect.setType(type);
                    }

                }

                PageInfo<ZhllCollect> zhllCollectPageInfo= zhllCollectService.findPage(zhllCollect,page,rows);
                Map<String,Object> resultMap = new HashMap<>();
                resultMap.put("total",zhllCollectPageInfo.getTotal());
                resultMap.put("items",zhllCollectPageInfo.getList());
                return ResponseUtil.ok(resultMap);
            }else{
                return ResponseUtil.fail401();
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("根据用户id 分页查询收藏的事项异常:",e );
            return ResponseUtil.fail();
        }

    }

    /**
     * @descption: 取消收藏
     * @param: [id]
     * @return: java.lang.Object
     * @auther: qxp
     * @date:19-7-19 16:18
     */
    @RequestMapping("/cancel")
    @ApiOperation(value = "根据条件删除删除收藏",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value="用户id",required=true, dataType = "String"),
            @ApiImplicitParam(name="serviceUnid",value="事项Unid",required=true, dataType = "String"),
            @ApiImplicitParam(name="type",value="事项类型",required=true, dataType = "String"),
    })
    public Object deleteById(String userId,String serviceUnid,String type){
        try{
            if(userId!=null&&!userId.trim().equals("")
            &&serviceUnid!=null&&!serviceUnid.trim().equals("")
            &&type!=null&&!type.trim().equals("")){
                ZhllCollect param =  new ZhllCollect();
                param.setUserId(userId);
                param.setServiceUnid(serviceUnid);
                param.setType(type);
                List<ZhllCollect> resultList= zhllCollectService.selectZhllCollectList(param);
                if(resultList.size()>0){
                    for (ZhllCollect zhllCollect:resultList) {
                        zhllCollectService.deleteByid(zhllCollect.getId());
                    }
                    return ResponseUtil.ok();
                }else{
                    return ResponseUtil.fail511();
                }
            }else{
                return ResponseUtil.fail402();
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("根据收藏的id删除收藏异常:",e );
            return ResponseUtil.fail();
        }
    }


}
