package com.chy.borrow.controller;


import com.chy.borrow.entity.BorrowApply;
import com.chy.borrow.entity.BorrowDownload;
import com.chy.borrow.eum.BorrowStatusEnum;
import com.chy.borrow.service.BorrowApplyService;
import com.chy.borrow.service.BorrowDownloadService;
import com.shanghaigeography.entity.ResultMsg;
import com.shanghaigeography.eum.ResultState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import com.chy.borrow.controller.BaseController;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 沈晔浩
 * @since 2022-04-07
 */
@Controller
@CrossOrigin
@RequestMapping("/borrowDownload")
public class BorrowDownloadController extends BaseController {

    @Autowired
    BorrowDownloadService borrowDownloadService;

    @Autowired
    BorrowApplyService borrowApplyService;

    @RequestMapping("/list")
    @ResponseBody
    public String list(@RequestBody Map map) {
        return borrowDownloadService.getList(map);
    }

    @RequestMapping("/download")
    @ResponseBody
    public String download(@RequestBody BorrowDownload borrowDownload) {
        try {
            BorrowDownload borrowDownload1= borrowDownloadService.getById(borrowDownload.getId());
            borrowDownload1.setDownloadTime(new Date());
            borrowDownload1.setDownloadStaus("2");
            borrowDownloadService.updateById(borrowDownload1);
            List result = new ArrayList();
            Map map = new HashMap();
            map.put("downloadUrl",borrowDownload1.getDownloadUrl());
            result.add(map);
            check(borrowDownload1.getBorrowApplyId());
            return new ResultMsg<>().setCode(ResultState.SUCCESS).setMsg("确认获取成功").setData(result).toJSON();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("确认获取失败").setData(null).toJSON();
        }
    }

    @RequestMapping("/obtain")
    @ResponseBody
    public String obtain(@RequestBody BorrowDownload borrowDownload) {
        try {
            BorrowDownload borrowDownload1 = new BorrowDownload();
            borrowDownload1.setId(borrowDownload.getId());
            borrowDownload1.setDownloadStaus("2");
            borrowDownload1.setDownloadTime(new Date());
            borrowDownloadService.updateById(borrowDownload1);
            BorrowDownload borrowDownload2= borrowDownloadService.getById(borrowDownload.getId());
            check(borrowDownload2.getBorrowApplyId());
            return new ResultMsg<String>().setCode(ResultState.SUCCESS).setMsg("确认获取成功").toJSON();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("确认获取失败").toJSON();
        }
    }

    private void check(String applyId){
        Map map = new HashMap();
        map.put("borrow_download.borrow_apply_id",applyId);
        List<BorrowDownload> list= borrowDownloadService.listByMap(map);
        for(BorrowDownload borrowDownload : list){
            if (borrowDownload.getDownloadStaus().equals("1")){
                return;
            }
        }
        BorrowApply borrowApply = new BorrowApply();
        borrowApply.setBorrowApplyId(applyId);
        borrowApply.setBorrowStatus(BorrowStatusEnum.STATUS_CONFIRM_DOWNLOAD.getStatusCodeString());
        borrowApplyService.updateById(borrowApply);
    }
}
