package com.template.controller;


import com.alibaba.fastjson.JSON;
import com.template.api.AlumniReturnControllerAPI;
import com.template.common.utils.TimeExchange;
import com.template.common.utils.WeiXiaoUtil;
import com.template.common.utils.paramUtils;
import com.template.model.enumModel.ePassType;
import com.template.model.enumModel.ePreferredMethod;
import com.template.model.enumModel.eSchoolType;
import com.template.model.pojo.AlumniReturn;
import com.template.model.pojo.AlumniUser;
import com.template.model.request.insertReturnRequest;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.model.vo.ApplicationRecordPageVo;
import com.template.model.vo.ApplicationReturnPageVo;
import com.template.model.vo.ListDataVO;
import com.template.services.AlumniReturnService;
import com.template.services.AlumniUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-12-03
 */
@RestController
public class AlumniReturnController implements AlumniReturnControllerAPI {
    @Autowired
    private WeiXiaoUtil weiXiaoUtil;
    @Autowired
    private AlumniUserService alumniUserService;
    @Autowired
    private AlumniReturnService alumniReturnService;
    private static Logger logger = LoggerFactory.getLogger(AlumniReturnController.class);

    @Override
    public CommonResult insertReturnData(insertReturnRequest ier, Integer userId, BindingResult bindingResult) {
        if (ier == null) {
            return CommonResult.fail("请传递参数");
        }
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        AlumniUser au = alumniUserService.queryUserByCnp(ier.getCardNumber(), ier.getName(), ier.getPhone());
        if (au == null) {
            return CommonResult.fail("人员信息无效，无法发起申请");
        }



        AlumniReturn ar = new AlumniReturn();
        ar.setSchoolId(ier.getSchoolId());
        ar.setName(ier.getName());
        ar.setCardNumber(ier.getCardNumber());
        ar.setPhone(ier.getPhone());
        ar.setUserId(au.getId());
        ar.setReturnsTime(TimeExchange.StringToDate(ier.getReturnsTime(), "yyyy-MM-dd"));
        ar.setReturneTime(TimeExchange.StringToDate(ier.getReturneTime(), "yyyy-MM-dd"));
        ar.setIsPass(ePassType.Passed.getValue());
        ar.setRemark(ier.getRemark());

        int result = alumniReturnService.insertAlumniReturn(ar);
        if(result <= 0){
            return CommonResult.fail("发起失败");
        }

        //发送微校通知
        //返校申请：当校友提交返校申请后，系统根据申请信息生成通知内容发生给超级管理员。
        //例如：“[申请人姓名]，返校申请已提交，申请信息如下：返校时间为 [返校时间]，返校原因是 [返校原因]，离校时间为 [离校时间]。”
        List<AlumniUser> users= alumniUserService.querySuperAdmins();
        List<String> cardnumbers= new ArrayList<>();
        for (AlumniUser user:users) {
            if(StringUtils.hasText(user.getCardNumber())){
                cardnumbers.add(user.getCardNumber());
            }
        }
        if(cardnumbers != null && cardnumbers.size() > 0){
            String content = ier.getName()+"，返校申请已提交，申请信息如下：返校时间为 "+ ier.getReturnsTime()+"，返校原因是 "+ier.getRemark()+"，离校时间为 "+ier.getReturneTime();
            CommonResult sendMessage = weiXiaoUtil.sendNotice(cardnumbers, "返校申请", "返校申请", content, null);
            if (!sendMessage.getCode().equals("200")) {
                logger.info("微校通知发送失败，cardnumber："+ JSON.toJSON(cardnumbers)+"，Content："+content);
            }
        }
        return CommonResult.ok("发起成功");
    }

    @Override
    public CommonResult applicationReturn(int currentPage, int pageCount, Integer userId, int weekType) {
        PageUtils<ApplicationReturnPageVo> result = alumniReturnService.applicationReturnDatas(currentPage, pageCount, userId, weekType);

        for (ApplicationReturnPageVo arpv : result.getList()) {
            arpv.setSchoolName(eSchoolType.stringOf(arpv.getSchoolId()));
            arpv.setPassName(ePassType.stringOf(arpv.getIsPass()));
            arpv.setReturnTime(arpv.getReturnsTime()+"-"+arpv.getReturneTime());
        }

        return CommonResult.ok(result);
    }

    @Override
    public CommonResult querySchools() {
        List<ListDataVO> result = new ArrayList<>();
        for (int i = 0; i <= eSchoolType.values().length; i++) {
            ListDataVO data = new ListDataVO();
            data.setId(i);
            data.setName(ePassType.stringOf(i));
            result.add(data);
        }

        return CommonResult.ok(result);
    }

}

