package com.izouma.awesomeadmin.service.impl;

import java.util.*;

import com.izouma.awesomeadmin.dao.StoreInfoMapper;
import com.izouma.awesomeadmin.dao.UserInfoMapper;
import com.izouma.awesomeadmin.model.StoreInfo;
import com.izouma.awesomeadmin.model.UserInfo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.model.PersonalAuthentication;
import com.izouma.awesomeadmin.service.PersonalAuthenticationService;
import com.izouma.awesomeadmin.dao.PersonalAuthenticationMapper;

/**
 * service接口实现类
 */
@Service
public class PersonalAuthenticationServiceImpl implements PersonalAuthenticationService {

    private static Logger logger = Logger.getLogger(PersonalAuthenticationServiceImpl.class);

    @Autowired
    private PersonalAuthenticationMapper personalAuthenticationMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private StoreInfoMapper storeInfoMapper;


    @Override
    public List<PersonalAuthentication> getPersonalAuthenticationList(PersonalAuthentication record) {

        logger.info("getPersonalAuthenticationList");
        try {

            return personalAuthenticationMapper.queryAllPersonalAuthentication(record);
        } catch (Exception e) {
            logger.error("getPersonalAuthenticationList", e);
        }

        return null;
    }

    @Override
    public List<PersonalAuthentication> getPersonalAuthenticationByPage(Page page, PersonalAuthentication record) {

        logger.info("getPersonalAuthenticationByPage");
        try {

            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("record", record);
            parameter.put(AppConstant.PAGE, page);

            return personalAuthenticationMapper.queryPersonalAuthenticationByPage(parameter);
        } catch (Exception e) {
            logger.error("getPersonalAuthenticationByPage", e);
        }

        return null;
    }

    @Override
    public PersonalAuthentication getPersonalAuthenticationById(String id) {

        logger.info("getPersonalAuthenticationyId");
        try {

            return personalAuthenticationMapper.selectByPrimaryKey(Integer.valueOf(id));
        } catch (Exception e) {
            logger.error("getPersonalAuthenticationById", e);
        }

        return null;
    }

    @Override
    public PersonalAuthentication getPersonalAuthentication(PersonalAuthentication record) {

        logger.info("getPersonalAuthentication");
        try {

            return personalAuthenticationMapper.queryPersonalAuthentication(record);
        } catch (Exception e) {
            logger.error("getPersonalAuthentication", e);
        }

        return null;
    }

    @Override
    public boolean createPersonalAuthentication(PersonalAuthentication record) {

        logger.info("createPersonalAuthentication");
        try {

            int updates = personalAuthenticationMapper.insertSelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("createPersonalAuthentication", e);
        }

        return false;
    }

    @Override
    public boolean deletePersonalAuthentication(String id) {

        logger.info("deletePersonalAuthentication");
        try {

            int updates = personalAuthenticationMapper.delete(id);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("deletePersonalAuthentication", e);
        }

        return false;
    }

    @Override
    public boolean updatePersonalAuthentication(PersonalAuthentication record) {

        logger.info("updatePersonalAuthentication");
        try {

            int updates = personalAuthenticationMapper.updateByPrimaryKeySelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("updatePersonalAuthentication", e);
        }

        return false;
    }

    @Override
    public boolean pass(PersonalAuthentication record) {

        logger.info("passPersonalAuthentication,通过开店申请");
        try {

            PersonalAuthentication personalAuthentication = personalAuthenticationMapper.selectByPrimaryKey(record.getId());

            if (personalAuthentication != null) {

                if (personalAuthentication.getStatusFlag() == 1) {
                    return true;
                }

                UserInfo userInfo = new UserInfo();
                userInfo.setId(personalAuthentication.getUserId());
                userInfo.setApproveFlag("Y");
                userInfo.setStoreFlag("Y");
                userInfo.setApproveStep(5);
                userInfoMapper.updateByPrimaryKeySelective(userInfo);//更新用户状态。

                Map<String, Object> parameter = new HashMap<String, Object>();
                parameter.put("userId", personalAuthentication.getUserId());
                parameter.put("useFlag", "Y");
                parameter.put("typeFlag", 2);//个人店铺

                storeInfoMapper.updateUseFlagByUserId(parameter);//更新店铺为可用

                record.setStatusFlag(1);//通过
                int updates = personalAuthenticationMapper.updateByPrimaryKeySelective(record);

                if (updates > 0) {
                    return true;
                }
            }


        } catch (Exception e) {
            logger.error("passPersonalAuthentication,通过开店申请异常", e);
        }

        return false;
    }

    @Override
    public boolean fail(PersonalAuthentication record) {

        logger.info("failPersonalAuthentication,失败开店申请");
        try {

            PersonalAuthentication personalAuthentication = personalAuthenticationMapper.selectByPrimaryKey(record.getId());

            if (personalAuthentication != null) {

                if (personalAuthentication.getStatusFlag() == 0) {//审核中才能失败

                    UserInfo userInfo = new UserInfo();
                    userInfo.setId(personalAuthentication.getUserId());
                    userInfo.setApproveFlag("N");
                    userInfo.setStoreFlag("N");
                    userInfo.setApproveStep(1);
                    userInfoMapper.updateByPrimaryKeySelective(userInfo);//更新用户状态。

                    Map<String, Object> parameter = new HashMap<String, Object>();
                    parameter.put("userId", personalAuthentication.getUserId());
                    parameter.put("useFlag", "N");
                    parameter.put("typeFlag", 2);//个人店铺

                    storeInfoMapper.updateUseFlagByUserId(parameter);//更新店铺为不可用

                    record.setStatusFlag(2);//失败
                    int updates = personalAuthenticationMapper.updateByPrimaryKeySelective(record);

                    if (updates > 0) {
                        return true;
                    }
                }


            }


        } catch (Exception e) {
            logger.error("failPersonalAuthentication,失败开店申请异常", e);
        }

        return false;
    }
}

