package com.bc.exam.modules.tjhxxb.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bc.exam.core.api.ApiRest;
import com.bc.exam.core.api.controller.BaseController;
import com.bc.exam.core.api.dto.PagingReqDTO;
import com.bc.exam.core.exception.ServiceException;
import com.bc.exam.modules.tjhxxb.entity.TJhxxb;
import com.bc.exam.modules.tjhxxb.service.TJhxxbService;
import com.bc.exam.modules.tlog.entity.TLog;
import com.bc.exam.modules.tlog.service.TLogService;
import com.bc.exam.modules.tsjxxb.entity.TSjxxb;
import com.bc.exam.modules.tsjxxb.service.TSjxxbService;
import com.bc.exam.modules.user.UserUtils;
import io.swagger.annotations.ApiOperation;
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 javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description 描述:借还信息表前端控制器
 * @Author A贾宇婷034244310
 * @Date 20260615
 */
@RestController
@RequestMapping("/tjhxxb/controller")
public class TJhxxbController extends BaseController {

    @Resource
    TJhxxbService jhxxbService;

    @Resource
    TLogService tLogService;

    @Resource
    TSjxxbService tSjxxbService;

    //图书借还信息表，表示每个人借还的当前状态 未归还和已归还

    /**
     * 借出书籍
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "借出书籍")
    @PostMapping("/jiechu")
    public ApiRest saveJc(@RequestBody TJhxxb reqDTO) {
        reqDTO.setState("0");//借还状态0 待归还
        reqDTO.setExceedState("0");//是否逾期,0未逾期
        reqDTO.setStartTime(new Date());//借阅时间
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DAY_OF_MONTH, 30);
        Date sDate = c.getTime();
        reqDTO.setEndTime(sDate);//应归还时间
        reqDTO.setUserId(UserUtils.getUserId());
        reqDTO.setId("");
        reqDTO.setMessage(reqDTO.getUserId()+"借出"+reqDTO.getBId());
        jhxxbService.save(reqDTO);

        //库存数量减少1
        TSjxxb tSjxxb= tSjxxbService.getById(reqDTO.getBId());
        if(0==tSjxxb.getBNum()){
            throw new ServiceException("库存数量不足！");
        }
        tSjxxb.setBNum(tSjxxb.getBNum()-1);
        //未归还数量加1
        tSjxxb.setBNotReturnNum(tSjxxb.getBNotReturnNum()+1);

        if(null==tSjxxb.getBLendNum()){
            tSjxxb.setBLendNum(0);
        }
        //借阅次数加1
        tSjxxb.setBLendNum(tSjxxb.getBLendNum()+1);
        tSjxxbService.updateById(tSjxxb);
        //日志 借还记录
        TLog log=new TLog();
        log.setUserid(UserUtils.getUserId());
        log.setId(UserUtils.getUserId());
        log.setState("1");//1借出2归还
        log.setBookname(tSjxxbService.getById(reqDTO.getBId()).getBName());//书名
        tLogService.save(log);

        return super.success();
    }

    /**
     * 还书籍
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "还书籍")
    @PostMapping("/guihuan")
    public ApiRest saveHs(@RequestBody TJhxxb reqDTO) {
        reqDTO.setState("1");//借还状态1已归还
        reqDTO.setExceedState("0");//是否逾期,0未逾期
        reqDTO.setMessage("未逾期");
        TJhxxb byId = jhxxbService.getById(reqDTO.getId());
        int i = differentDays(byId.getStartTime(), new Date());
        if(i>30){
            reqDTO.setExceedState("1");//是否逾期,1逾期
            reqDTO.setMessage("逾期"+i+"天");
        }
        reqDTO.setReturnTime(new Date());
        reqDTO.setUserId(UserUtils.getUserId());
        jhxxbService.saveOrUpdate(reqDTO);

        //书籍信息
        TSjxxb tSjxxb= tSjxxbService.getById(reqDTO.getBId());
        //bNotReturnNum -1未归还数量
        //bNum +1 库存数量
        tSjxxb.setBNotReturnNum(tSjxxb.getBNotReturnNum()-1);
        tSjxxb.setBNum(tSjxxb.getBNum()+1);
        tSjxxbService.saveOrUpdate(tSjxxb);


        //日志 借还记录
        TLog log=new TLog();
        log.setId(UserUtils.getUserId());
        log.setUserid(UserUtils.getUserId());
        log.setState("2");//1借出2归还
        log.setBookname(tSjxxbService.getById(reqDTO.getBId()).getBName());//书名
        tLogService.save(log);

        return super.success();
    }



    /**
     * 查询个人书籍状态
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查询个人书籍状态")
    @PostMapping("/queryUserSj")
    public ApiRest<IPage<TJhxxb>> query(@RequestBody PagingReqDTO<TJhxxb> reqDTO) {
        reqDTO.setUserId(UserUtils.getUserId());
        //分页查询并转换
        IPage<TJhxxb> page = jhxxbService.paging(reqDTO);
        return super.success(page);
    }



    /**
     * date2比date1多的天数
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1,Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2) //同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0) //闰年
                {
                    timeDistance += 366;
                }
                else //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        }
        else //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }

}

