package com.xxfc.platform.vehicle.rest;

import com.github.wxiaoqi.security.auth.client.annotation.IgnoreClientToken;
import com.github.wxiaoqi.security.auth.client.annotation.IgnoreUserToken;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.util.process.ResultCode;
import com.github.wxiaoqi.security.common.vo.PageDataVO;
import com.xxfc.platform.vehicle.biz.BranchCompanyStockApplyInfoBiz;
import com.xxfc.platform.vehicle.biz.BranchCompanyStockRightBiz;
import com.xxfc.platform.vehicle.common.BaseController;
import com.xxfc.platform.vehicle.common.RestResponse;
import com.xxfc.platform.vehicle.constant.ResCode.ResCode;
import com.xxfc.platform.vehicle.entity.BranchCompanyStockApplyInfo;
import com.xxfc.platform.vehicle.pojo.BranchCompanyStockApplyVo;
import com.xxfc.platform.vehicle.pojo.BranchCompanyStockSearchVo;
import com.xxfc.platform.vehicle.pojo.dto.BranchCompanyStockRightFindDTO;
import com.xxfc.platform.vehicle.pojo.vo.BranchCompanyStockInfoRightVo;
import com.xxfc.platform.vehicle.pojo.vo.BranchCompanyStockRightForWeChatOfficeVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@RestController
@IgnoreClientToken
@IgnoreUserToken
@RequestMapping("branchCompany/stock/right")
public class BranchCompanyStockRightController extends BaseController<BranchCompanyStockRightBiz> {


    @Autowired
    BranchCompanyStockApplyInfoBiz branchCompanyStockApplyInfoBiz;



    /**
     * 搜索
     * @param searchVo
     * @return
     */
    @ApiOperation(value = "获取股权列表")
    @GetMapping("search")
    public RestResponse search(BranchCompanyStockSearchVo searchVo) {
        if (searchVo.getPage() == null || searchVo.getLimit() == null) {
            searchVo.setPage(1);
            searchVo.setLimit(10);
        }
        return RestResponse.suc(baseBiz.search(searchVo));
    }

    @ApiOperation(value = "获取股权信息")
    @GetMapping("info/{id}")
    public RestResponse getInfo(@PathVariable("id") Integer id) {
        if (id == null) {
            return RestResponse.codeAndMessage(ResCode.INVALID_REST_REQ_PARAM.getCode(),
                    ResCode.INVALID_REST_REQ_PARAM.getDesc());
        }
        return RestResponse.suc(baseBiz.getInfoById(id));
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value ="/updStockInfo",method = RequestMethod.POST)
    public ObjectRestResponse updStockInfo(@RequestBody BranchCompanyStockInfoRightVo stockInfoVo) {
        if (stockInfoVo==null||stockInfoVo.getCompanyId()==null||stockInfoVo.getCompanyId()==0 || stockInfoVo.getTotal() == null || stockInfoVo.getPrice() == null) {
             return  ObjectRestResponse.createFailedResult(ResultCode.NULL_CODE,"参数不能为空");
        }
        return baseBiz.updStockInfo(stockInfoVo);
    }

    /**
     * 新增
     * @return
     */
    @RequestMapping(value ="/addStockInfo",method = RequestMethod.POST)
    public ObjectRestResponse addStockInfo(@RequestBody BranchCompanyStockInfoRightVo stockInfoVo) {
        if (stockInfoVo==null||stockInfoVo.getCompanyId()==null||stockInfoVo.getCompanyId()==0|| stockInfoVo.getTotal() == null || stockInfoVo.getPrice() == null) {
            return  ObjectRestResponse.createFailedResult(ResultCode.NULL_CODE,"参数不能为空");
        }
        return baseBiz.updStockInfo(stockInfoVo);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ObjectRestResponse delete(@PathVariable("id") Integer id) {
         baseBiz.delete(id);
        return ObjectRestResponse.succ();
    }


    /**
     * 申请购买
     * @param applyVo
     * @return
     */
    @PostMapping("/app/unauth/apply")
    public RestResponse apply(@RequestBody BranchCompanyStockApplyVo applyVo) {
        if (applyVo.getStockRightId() == null || applyVo.getName() == null || applyVo.getTel() == null || applyVo.getCount() == null) {
            return RestResponse.codeAndMessage(ResCode.INVALID_REST_REQ_PARAM.getCode(),
                    ResCode.INVALID_REST_REQ_PARAM.getDesc());
        }
        branchCompanyStockApplyInfoBiz.apply(applyVo);
        return RestResponse.suc();
    }

    @GetMapping("apply/page")
    public RestResponse applyPage(Integer page, Integer limit) {
        if (page == null || limit == null) {
            page = 1;
            limit = 10;
        }
        return RestResponse.suc(branchCompanyStockApplyInfoBiz.selectApplyAll(page, limit));
    }

    @GetMapping("apply/{id}")
    public RestResponse getApplyById(@PathVariable("id") Integer id) {
        if (id == null) {
            return RestResponse.codeAndMessage(ResCode.INVALID_REST_REQ_PARAM.getCode(),
                    ResCode.INVALID_REST_REQ_PARAM.getDesc());
        }
        return RestResponse.suc(branchCompanyStockApplyInfoBiz.getApplyById(id));
    }

    @PutMapping("apply")
    public RestResponse updateApply(BranchCompanyStockApplyInfo applyInfo) {
        if (applyInfo.getId() == null) {
            return RestResponse.codeAndMessage(ResCode.INVALID_REST_REQ_PARAM.getCode(),
                    ResCode.INVALID_REST_REQ_PARAM.getDesc());
        }
        branchCompanyStockApplyInfoBiz.updateApply(applyInfo);
        return RestResponse.suc();
    }

    @DeleteMapping("apply/{id}")
    public RestResponse deleteApply(@PathVariable("id") Integer id) {
        if (id == null) {
            return RestResponse.codeAndMessage(ResCode.INVALID_REST_REQ_PARAM.getCode(),
                    ResCode.INVALID_REST_REQ_PARAM.getDesc());
        }
        branchCompanyStockApplyInfoBiz.deleteApply(id);
        return RestResponse.suc();
    }

    /**
     * 取消申请，放弃购买
     * @return
     */
    @PostMapping("apply/cancel")
    public RestResponse cancel(Integer applyId) {
        if (applyId == null) {
            return RestResponse.codeAndMessage(ResCode.INVALID_REST_REQ_PARAM.getCode(),
                    ResCode.INVALID_REST_REQ_PARAM.getDesc());
        }
        branchCompanyStockApplyInfoBiz.cancelApply(applyId);
        return RestResponse.suc();
    }

    /**
     * 确认购买，减库存
     */
    @PostMapping("apply/buy")
    public RestResponse buy(Integer applyId) {
        if (applyId == null) {
            return RestResponse.codeAndMessage(ResCode.INVALID_REST_REQ_PARAM.getCode(),
                    ResCode.INVALID_REST_REQ_PARAM.getDesc());
        }
        branchCompanyStockApplyInfoBiz.buy(applyId);
        return RestResponse.suc();
    }

    @GetMapping("allPrice")
    public RestResponse getAllPrice() {
        return RestResponse.suc(baseBiz.getAllPrice());
    }


    /**
     * 导入
     * @param multipartfile
     * @param request
     * @return
     */
    @PostMapping("importExcel")
    public ObjectRestResponse<String> importExcel(@RequestParam(value = "file") MultipartFile multipartfile,
                                                  HttpServletRequest request) {
        return baseBiz.importExcel(multipartfile,request);
    }

    @GetMapping("/app/unauth/stock_right_price")
    public ObjectRestResponse<List<Integer>> getStorkRightPrices(){
        List<Integer> allPrice = baseBiz.getAllPrice();
        return ObjectRestResponse.succ(allPrice);
    }

    /**
     * 微信公众号查询
     * @param branchCompanyStockRightFindDTO
     * @return
     */
    @PostMapping("/app/unauth/webchat_office/stockrights")
    public ObjectRestResponse<PageDataVO<BranchCompanyStockRightForWeChatOfficeVO>> findBranchCompanyStockRightsForWechatOffice(@RequestBody  BranchCompanyStockRightFindDTO branchCompanyStockRightFindDTO){
         PageDataVO<BranchCompanyStockRightForWeChatOfficeVO> branchCompanyStockRightForWeChatOfficeVOPageDataVO = baseBiz.findWithPageByPriceOrCode(branchCompanyStockRightFindDTO);
        return ObjectRestResponse.succ(branchCompanyStockRightForWeChatOfficeVOPageDataVO);
    }

}
