package com.winsun.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.bean.AntiFraudNotice;
import com.winsun.mapper.AntiFraudNoticeMapper;
import com.winsun.utils.PicturesUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 反诈通知公告
 */
@Slf4j
@RestController
@RequestMapping("/antiFraud") //
public class AntiFraudController {

    @Autowired
    private AntiFraudNoticeMapper antiFraudNoticeMapper;

    private static String PATH1 = "AntiFraud1";

    /**
     * 学生反诈通告签字上传
     *
     * @param userName 用户名
     * @param orderPhone 办理号码
     * @param linkPhone 联系号码
     * @param file1 签字图片
     * @return
     */
    @RequestMapping(value = "uploadSign", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData<String> uploadStuCard(String userName, String orderPhone, String linkPhone, String file1) {

        if (StringUtils.isBlank(userName)) {
            return ResponseData.error("请填写姓名");
        }

        if (StringUtils.isBlank(orderPhone)) {
            return ResponseData.error("请填写办理号码");
        }

        if (StringUtils.isBlank(linkPhone)) {
            return ResponseData.error("请填写联系号码");
        }

        Wrapper<AntiFraudNotice> wrapper = new EntityWrapper<>();
        wrapper.eq("order_phone", orderPhone);
        wrapper.eq("link_phone", linkPhone);
        AntiFraudNotice notice = new AntiFraudNotice();
        notice.setUserName(userName);
        notice.setOrderPhone(orderPhone);
        notice.setLinkPhone(linkPhone);
        notice.setUploadDate(new Date());
        if (StringUtils.isNotBlank(file1)) {
            String file01 = PicturesUtil.uploadPictures(file1, orderPhone, PATH1);
            notice.setOtherImgUrl1(file01);
        }
        Integer integer1 = antiFraudNoticeMapper.update(notice, wrapper);
        if (integer1 == 1) {
            return ResponseData.success("更新成功！");
        } else {
            notice.setIsFirstUpload(1);
            notice.setFirstUploadDate(new Date());
            antiFraudNoticeMapper.insert(notice);
            return ResponseData.success("上传成功！");
        }
    }

    /**
     * 学生反诈通告签字上传
     *
     * @param userName 用户名(可空)
     * @param orderPhone 办理号码(可空)
     * @param linkPhone 联系号码(可空)
     * @param file1 签字图片
     * @return
     */
    @RequestMapping(value = "uploadSign2", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData<String> uploadStuCard2(String userName, String orderPhone, String linkPhone, String file1) {

        AntiFraudNotice notice = new AntiFraudNotice();
        notice.setUserName(userName);
        notice.setOrderPhone(orderPhone);
        notice.setLinkPhone(linkPhone);
        notice.setUploadDate(new Date());
        if (StringUtils.isNotBlank(file1)) {
            String file01 = PicturesUtil.uploadPictures(file1, orderPhone, PATH1);
            notice.setOtherImgUrl1(file01);
        }
        notice.setIsFirstUpload(1);
        notice.setFirstUploadDate(new Date());
        try{
            antiFraudNoticeMapper.insert(notice);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.error("上传失败！");
        }
        return ResponseData.success("上传成功！");

    }
}
