package com.pj.tb_goods_demand;

import com.pj.common.core.exception.ServiceException;
import com.pj.enummj.DeleteStatus;
import com.pj.enummj.IsOrders;
import com.pj.project4sp.SP;
import com.pj.tb_demand_quotation.TbDemandQuotationMapper;
import com.pj.tb_goods_demand.vo.GoodsDemandVo;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * Controller: tb_goods_demand -- 二级收购商需求发布表
 *
 * @author yzs
 */
@RestController
@RequestMapping("/app/TbGoodsDemand/")
public class AppTbGoodsDemandController {

    /**
     * 底层 Service 对象
     */
    @Autowired
    TbGoodsDemandService tbGoodsDemandService;


    /**
     * 二级市场
     */
    @RequestMapping("getList")
    public AjaxJson getList() {
        SoMap so = SoMap.getRequestSoMap();
        so.put("isOrders", 0);
        so.put("isRelease", 1);
        so.put("deleteStatus", DeleteStatus.DELETE_STATUS_ON.getCode());
        List<TbGoodsDemand> list = tbGoodsDemandService.getList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    /**
     * 增
     */
    @RequestMapping("add")
    public AjaxJson add(TbGoodsDemand t) {
        tbGoodsDemandService.add(t);
        return AjaxJson.getSuccess();
    }

    /**
     * 发布|取消发布
     */
    @PostMapping("changeState")
    public AjaxJson changeState(TbGoodsDemand dto) {
        //取消发布前检查需求是否已被报价
        TbGoodsDemand goodsDemand = tbGoodsDemandService.getById(dto.getId());
        if(goodsDemand == null)throw new ServiceException("需求状态异常!");
        //判断是否已被报价
        if(goodsDemand.getIsOrders().equals(IsOrders.IS_ORDERS_ONE.getCode()))
            throw new ServiceException("您的需求采购[ " + goodsDemand.getGoodsName() + " ]已被报价,请处理报价再进行取消发布。");
        TbGoodsDemand t = new TbGoodsDemand();
        t.setId(dto.getId());
        t.setIsRelease(dto.getIsRelease());
        return AjaxJson.toAjax(tbGoodsDemandService.updateById(t));
    }

    /**
     * 二级市场收购商删除需求 -- 单个
     */
    @RequestMapping("delete")
    public AjaxJson delete(Long id) {
        tbGoodsDemandService.delete(id);
        return AjaxJson.getSuccess();
    }

    /**
     * 删 - 根据id列表
     */
    @RequestMapping("deleteByIds")
    public AjaxJson deleteByIds() {
        List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class);
        int line = SP.publicMapper.deleteByIds(TbGoodsDemand.TABLE_NAME, ids);
        return AjaxJson.getByLine(line);
    }

    /**
     * 改
     */
    @RequestMapping("update")
    public AjaxJson update(TbGoodsDemand t) {
        tbGoodsDemandService.update(t);
        return AjaxJson.getSuccess();
    }

    /**
     * 查 - 根据id
     */
    @RequestMapping("getById")
    public AjaxJson getById(Long id) {
        TbGoodsDemand t = tbGoodsDemandService.getById(id);
        return AjaxJson.getSuccessData(t);
    }

    /**
     * app端:专供给一级市场查看的需求列表
     */
    @RequestMapping("getAppList")
    public AjaxJson getAppList() {
        SoMap so = SoMap.getRequestSoMap();
        List<TbGoodsDemand> list = tbGoodsDemandService.getAppList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    /**
     * app端:二级收购商查看需求发布需求详情
     */
    @RequestMapping("goodsDemandDetail")
    public AjaxJson goodsDemandDetail() {
        SoMap so = SoMap.getRequestSoMap();
        GoodsDemandVo goodsDemandVos = tbGoodsDemandService.goodsDemandList(so);
        return AjaxJson.getSuccessData(goodsDemandVos);
    }

    /**
     * app端:  二级市场收购商处理已被报价的需求
     *
     * @param disposeStatus     商家处理状态,只能是1或者2,其他值会抛异常 , 1=同意,2=拒绝
     * @param goodsDemandId     需求表主键
     * @param demandQuotationId 报价表主键
     * @param purchaserRemark   商家的备注,非必填
     * @return true/false
     */
    @RequestMapping("disposeOrders")
    public AjaxJson disposeOrders(@RequestParam(value = "disposeStatus") Integer disposeStatus, @RequestParam("goodsDemandId") Long goodsDemandId, @RequestParam("demandQuotationId") Long demandQuotationId, @RequestParam(value = "purchaserRemark", required = false) String purchaserRemark) {
        boolean disposed = tbGoodsDemandService.disposeOrders(disposeStatus, goodsDemandId, demandQuotationId, purchaserRemark);
        if (disposed) return AjaxJson.getSuccess("处理报价结果成功!");
        return AjaxJson.getError("处理报价结果失败!");
    }

    /** 收购商拒绝接单，即收购商拒绝组长的报价，不生成二级市场订单 ，恢复发布需求的状态*/
    @RequestMapping("refuse")
    public AjaxJson refuse(TbGoodsDemand t) {
        return AjaxJson.toAjax(tbGoodsDemandService.refuse(t));
    }

}
