package com.dgtly.member.controller;

import com.dgtly.apiframework.util.TokenUtil;
import com.dgtly.common.annotation.ApiNoCheckSign;
import com.dgtly.common.annotation.ApiPassToken;
import com.dgtly.common.core.controller.ApiBaseController;
import com.dgtly.common.core.domain.AjaxResult;
import com.dgtly.common.core.domain.ParameterObject;
import com.dgtly.common.core.domain.ResultType;
import com.dgtly.common.utils.DateUtils;
import com.dgtly.common.utils.StringUtils;
import com.dgtly.notice.domain.NoticeDemand;
import com.dgtly.notice.service.INoticeDemandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
  * @descption: 需求、供应相关接口API
  * @author LiuLingChao
  * @version 1.0
  * @company 神州数码通用软件（洛阳）有限公司
  * @copyright (c) 2019 LuoYang DGT Co'Ltd Inc. All rights reserved.
  * @date 2020-2-25 10:12
  * @since JDK1.8
  */

@RestController
@RequestMapping("/demand/info")
@ApiNoCheckSign/* 不用验证 签名 方法上可用*/
public class DemandInfoController extends ApiBaseController {

     @Autowired
     private INoticeDemandService noticeDemandService;


     /**
     * @descption: 首页获取需求列表前10条
     * @param:
     * @return: 最新的需求信息前10条
     * @auther: LiuLingChao
     * @date: 2020-2-25 10:11
     */
    @ApiPassToken/* 不用验证token  方法上可用*/
    @PostMapping("/getDemandInfoTenList")
    public Object getDemandInfoTenList(){
        List<NoticeDemand> noticeDemandList = noticeDemandService.getDemandInfoTenList();
        return AjaxResult.success().putKV("demandList",noticeDemandList);

    }

    /**
     * @descption: 获取需求信息详情
     * @param: 需求id
     * @return:
     * @auther: LiuLingChao
     * @date: 2020-2-25 10:54
     */
    @ApiPassToken/* 不用验证token  方法上可用*/
    @PostMapping("/getDemandInfo")
    public Object getDemandInfo(){
        ParameterObject obj =  getParameterObject();
        NoticeDemand noticeDemand = obj.parseBean(NoticeDemand.class);
        NoticeDemand noticeDemand1 = noticeDemandService.selectNoticeDemandById(noticeDemand.getId());
        return AjaxResult.success().putKV("demandInfo",noticeDemand1);
    }

    /**
     * @descption: 获取全部需求信息分页列表
     * @param:
     * @return:
     * @auther: LiuLingChao
     * @date: 2020-2-25 11:02
     */
    @ApiPassToken/* 不用验证token  方法上可用*/
    @PostMapping("/getDemandInfoAllList")
    public Object getDemandInfoAllList(){
        ParameterObject obj =  getParameterObject();
        NoticeDemand noticeDemand = obj.parseBean(NoticeDemand.class);
        startPage(obj);/*向分页传递 分页参数*/
        List<NoticeDemand> noticeDemandList = noticeDemandService.selectNoticeDemandList(noticeDemand);
        return AjaxResult.success(getDataTable(noticeDemandList));
    }

    /**
     * @descption: 发布需求信息
     * @param:
     * @return:
     * @auther: LiuLingChao
     * @date: 2020-2-27 16:03
     */
    @PostMapping("/insertDemandInfo")
    public Object insertDemandInfo(){
        String userId = TokenUtil.getTokenUserId();
            ParameterObject obj =  getParameterObject();
            NoticeDemand noticeDemand = obj.parseBean(NoticeDemand.class);
            noticeDemand.setMemberId(Long.parseLong(userId));
            noticeDemand.setCreateTime(DateUtils.getNowDate());
            noticeDemand.setReleaseTime(DateUtils.getNowDate());
            int flag = noticeDemandService.insertNoticeDemand(noticeDemand);
            return toAjax(flag);
    }



    /**
     * @descption: 获取网站用户发布的需求
     * @param:
     * @return:
     * @auther: LiuLingChao
     * @date: 2020-2-27 16:08
     */
    @PostMapping("/getMemberDemandList")
    public Object getMemberDemandList(){
        String userId = TokenUtil.getTokenUserId();
            ParameterObject obj =  getParameterObject();
            startPage(obj);
            NoticeDemand noticeDemand = new NoticeDemand();

            noticeDemand.setMemberId(Long.parseLong(userId));
            List<NoticeDemand> noticeDemandList = noticeDemandService.selectNoticeDemandList(noticeDemand);
            return AjaxResult.success(getDataTable(noticeDemandList));
    }


    /**
     * @descption: 撤销需求
     * @param:
     * @return:
     * @auther: LiuLingChao
     * @date: 2020-2-27 16:17
     */
    @PostMapping("/cancleMemberDemandInfo")
    public Object cancleMemberDemandInfo(){
        ParameterObject obj =  getParameterObject();
        NoticeDemand noticeDemand = obj.parseBean(NoticeDemand.class);
        int flag = noticeDemandService.deleteNoticeDemandById(noticeDemand.getId());
        return toAjax(flag);
    }


}
