package com.liquidnet.service.kylin.service.impl.partner;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liquidnet.service.base.ErrorMapping;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.kylin.dao.ChildDetailsDao;
import com.liquidnet.service.kylin.dao.ChildListDao;
import com.liquidnet.service.kylin.entity.KylinCheckUser;
import com.liquidnet.service.kylin.mapper.KylinCheckUserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.service.kylin.service.partner.IKylinCheckUserPartnerService;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 验票用户表 服务实现类
 * </p>
 *
 * @author liquidnet
 * @since 2021-05-21
 */
@Service
public class KylinCheckUserPartnerServiceImpl extends ServiceImpl<KylinCheckUserMapper, KylinCheckUser> implements IKylinCheckUserPartnerService {

    @Autowired
    private KylinCheckUserMapper checkUserMapper;

    @Override
    public PageInfo<ChildListDao> getCheckUserList(String merchantId, String mobile, int page, int size) {
        PageInfo<ChildListDao> pageInfo = null;
        try {
            PageHelper.startPage(page, size);
            List<ChildListDao> data = checkUserMapper.childSimpleList(merchantId, mobile);
            pageInfo = new PageInfo(data);
            return pageInfo;
        } catch (Exception e) {
            return pageInfo;
        }
    }

    @Override
    public ResponseDto<String> del(String merchantId, String checkUserId) {
        try {
            KylinCheckUser checkUser = new KylinCheckUser();
            checkUser.setUpdatedAt(LocalDateTime.now());
            checkUser.setStatus(0);
            checkUserMapper.update(checkUser, new UpdateWrapper<KylinCheckUser>().eq("checkUserId", checkUserId));
            return ResponseDto.success("删除成功");
        } catch (Exception e) {
            return ResponseDto.failure(ErrorMapping.get(20102));
        }
    }

    @Override
    public ResponseDto<String> create(String merchantId, String name, String pwd, String mobile) {
        try {
            KylinCheckUser checkUser = new KylinCheckUser();
            checkUser.setName(name);
            checkUser.setPwd(DigestUtils.md5DigestAsHex(pwd.getBytes()));
            checkUser.setMobile(mobile);
            checkUser.setCreatedAt(LocalDateTime.now());
            checkUser.setMerchantId(merchantId);
            checkUser.setStatus(1);
            checkUserMapper.insert(checkUser);
            return ResponseDto.success("添加成功");
        } catch (Exception e) {
            return ResponseDto.failure(ErrorMapping.get(20101));
        }
    }

    @Override
    public ResponseDto<String> change(String merchantId, String checkUserId, String name, String pwd, String mobile) {
        try {
            KylinCheckUser checkUser = new KylinCheckUser();
            checkUser.setName(name);
            checkUser.setPwd(DigestUtils.md5DigestAsHex(pwd.getBytes()));
            checkUser.setMobile(mobile);
            checkUser.setUpdatedAt(LocalDateTime.now());
            checkUserMapper.update(checkUser, new UpdateWrapper<KylinCheckUser>().eq("checkUserId", checkUserId).eq("merchantId", merchantId));
            return ResponseDto.success("修改成功");
        } catch (Exception e) {
            return ResponseDto.failure(ErrorMapping.get(20103));
        }
    }

    @Override
    public ResponseDto<ChildDetailsDao> details(String merchantId, String checkUserId) {
        try {
            ChildDetailsDao data = checkUserMapper.childDetails(merchantId, checkUserId);
            return ResponseDto.success(data);
        } catch (Exception e) {
            return ResponseDto.failure(ErrorMapping.get(20104));
        }
    }
}
