package com.izouma.awesomeadmin.service.impl;

import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dao.CompanyAuthenticationMapper;
import com.izouma.awesomeadmin.dao.StoreInfoMapper;
import com.izouma.awesomeadmin.dao.UserInfoMapper;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.model.CompanyAuthentication;
import com.izouma.awesomeadmin.model.UserInfo;
import com.izouma.awesomeadmin.service.CompanyAuthenticationService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * service接口实现类
 */
@Service
public class CompanyAuthenticationServiceImpl implements CompanyAuthenticationService {

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

    @Autowired
    private CompanyAuthenticationMapper companyAuthenticationMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private StoreInfoMapper storeInfoMapper;


    @Override
    public List<CompanyAuthentication> getCompanyAuthenticationList(CompanyAuthentication record) {

        logger.info("getCompanyAuthenticationList");
        try {

            return companyAuthenticationMapper.queryAllCompanyAuthentication(record);
        } catch (Exception e) {
            logger.error("getCompanyAuthenticationList", e);
        }

        return null;
    }

    @Override
    public List<CompanyAuthentication> getCompanyAuthenticationByPage(Page page, CompanyAuthentication record) {

        logger.info("getCompanyAuthenticationByPage");
        try {

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

            return companyAuthenticationMapper.queryCompanyAuthenticationByPage(parameter);
        } catch (Exception e) {
            logger.error("getCompanyAuthenticationByPage", e);
        }

        return null;
    }

    @Override
    public CompanyAuthentication getCompanyAuthenticationById(String id) {

        logger.info("getCompanyAuthenticationyId");
        try {

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

        return null;
    }

    @Override
    public CompanyAuthentication getCompanyAuthentication(CompanyAuthentication record) {

        logger.info("getCompanyAuthentication");
        try {

            return companyAuthenticationMapper.queryCompanyAuthentication(record);
        } catch (Exception e) {
            logger.error("getCompanyAuthentication", e);
        }

        return null;
    }

    @Override
    public boolean createCompanyAuthentication(CompanyAuthentication record) {

        logger.info("createCompanyAuthentication");
        try {

            int updates = companyAuthenticationMapper.insertSelective(record);

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

        return false;
    }

    @Override
    public boolean deleteCompanyAuthentication(String id) {

        logger.info("deleteCompanyAuthentication");
        try {

            int updates = companyAuthenticationMapper.delete(id);

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

        return false;
    }

    @Override
    public boolean updateCompanyAuthentication(CompanyAuthentication record) {

        logger.info("updateCompanyAuthentication");
        try {

            int updates = companyAuthenticationMapper.updateByPrimaryKeySelective(record);

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

        return false;
    }

    @Override
    public boolean pass(CompanyAuthentication record) {

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

            CompanyAuthentication companyAuthentication = companyAuthenticationMapper.selectByPrimaryKey(record.getId());

            if (companyAuthentication != null) {

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

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

                Map<String, Object> parameter = new HashMap<String, Object>();
                parameter.put("userId", companyAuthentication.getUserId());
                parameter.put("useFlag", "Y");
                parameter.put("typeFlag", 1);//企业店铺

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

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

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


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

        return false;
    }

    @Override
    public boolean fail(CompanyAuthentication record) {

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

            CompanyAuthentication companyAuthentication = companyAuthenticationMapper.selectByPrimaryKey(record.getId());

            if (companyAuthentication != null) {

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

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

                    Map<String, Object> parameter = new HashMap<String, Object>();
                    parameter.put("userId", companyAuthentication.getUserId());
                    parameter.put("useFlag", "N");
                    parameter.put("typeFlag", 1);//企业店铺

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

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

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


            }


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

        return false;
    }
}

