package com.winsun.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.auth.core.shiro.ShiroUser;
import com.winsun.bean.AuthCode;
import com.winsun.bean.NetworkInfo;
import com.winsun.bean.SalesList;
import com.winsun.item.core.shiro.ShiroKit;
import com.winsun.mapper.AuthCodeMapper;
import com.winsun.mapper.NetworkInfoMapper;
import com.winsun.mapper.SalesListMapper;
import com.winsun.service.ISalesListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 放号登记
 * @author chengfengluo
 * @date 2021-03-08 15:15
 */
@RequestMapping("recordNumber")
@RestController
public class RecordNumberController extends BaseController {

    @Autowired
    @Qualifier("redisStringTemplate")
    private RedisTemplate redisTemplate;

    @Autowired
    private SalesListMapper salesListMapper;
    @Autowired
    private ISalesListService salesListService;

    @Autowired
    private AuthCodeMapper authCodeMapper;

    @Autowired
    private NetworkInfoMapper networkInfoMapper;

    /**
     * 放号登记注册
     * @param orderPhone
     * @param salesSubst
     * @param salesSchool
     * @param code
     * @return
     */
    @RequestMapping(name = "放号登记注册", value = "register", method = RequestMethod.POST)
    public ResponseData<String> register(@RequestParam("orderPhone") String orderPhone,
                                         @RequestParam("salesSubst") String salesSubst,
                                         @RequestParam("salesSchool") String salesSchool,
                                         @RequestParam("code")String code) {
        try{
            ShiroUser shiroUser = ShiroKit.getUser();
            SalesList salesList = new SalesList();
            // 登录用户校验
            if(shiroUser == null) {
                return ResponseData.error("请先登录。");
            }

            // 参数校验
            if (StringUtils.isBlank(orderPhone) ||
                    StringUtils.isBlank(salesSubst) ||
                    StringUtils.isBlank(salesSchool)) {
                return ResponseData.error("请填写号码、销售县分、销售学校。");
            }
            salesList.setOrderPhone(orderPhone);
            salesList.setSalesSubst(salesSubst);
            salesList.setSalesSchool(salesSchool);

            // 校验网点是否存在
            boolean isNotNetWork = false;
            Wrapper<NetworkInfo> wrapper = new EntityWrapper<>();
            wrapper.eq("school_name", salesSchool);
            Integer integer = networkInfoMapper.selectCount(wrapper);

            if(integer == null || integer == 0) {
                isNotNetWork = true;
            }

            // 校验验证码
            AuthCode authCode = authCodeMapper.selectById(orderPhone);
            Object o = redisTemplate.opsForValue().get(orderPhone);
            if (o == null && authCode == null) {
                return ResponseData.error("请先获取验证码");
            }
            if (!code.equals(o) && !code.equals(authCode.getUserAuthCode())) {
                return ResponseData.error("验证码错误，若未收到验证码，请登录后台查询");
            }

            // 添加上传人信息
            salesList.setStatus("1");
            salesList.setLockState("否");
            salesList.setOrderType("1");
            salesList.setPreType("");

            salesList.setAccount(shiroUser.getAccount());
            salesList.setUserId(String.valueOf(shiroUser.getId()));
            salesList.setName(shiroUser.getName());
            salesList.setCreateTime(new Date());
            salesList.setDelFlag("0");

            salesList.setMonthAmount(0D);
            salesList.setMonthBonus(0D);
            salesList.setNonSendBonus(0D);
            salesList.setSendBonus(0D);
            salesList.setAllBonus(0D);
            ResponseData<String> stringResponseData = salesListService.addSalesList(salesList);
            if (stringResponseData.isSuccess() && isNotNetWork) {
                stringResponseData.setData("该学校无网点，请联系县分管理员配置");
            }
            return stringResponseData;
        }catch(Exception e) {
            e.printStackTrace();
            return ResponseData.error("放号失败，请记录号码和验证码，后续上报登记。");
        }
    }

}
