package org.ccpit.business.registerManage;

import org.ccpit.base.controller.Convert;
import org.ccpit.base.controller.Page;
import org.ccpit.base.controller.PageBo;
import org.ccpit.base.controller.PageRequest;
import org.ccpit.base.metadataManage.MetadataService;
import org.ccpit.base.utils.TypeUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SubjectInfoService {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd MM:hh:ss");
    
    @Resource
    private SubjectInfoDao subjectInfoDao;
    @Resource
    private MetadataService metadataService;

    @Transactional
    public boolean addSubjectInfo(SubjectInfo subjectInfo) {
        subjectInfo.setSubmitTime(new Date());
        return subjectInfoDao.save(subjectInfo);
    }

    @Transactional
    public boolean updateSubjectInfo(SubjectInfo subjectInfo) {
        subjectInfo.setModifyTime(new Date());
        return subjectInfoDao.update(subjectInfo);
    }

    @Transactional
    public boolean deleteSubjectInfos(long id) {
        return subjectInfoDao.deleteById(id);
    }

    public SubjectInfo querySubjectInfoById(long id) {
        return subjectInfoDao.queryById(id);
    }


    public List<SubjectInfo> queryList(String hql) {
        return subjectInfoDao.query(hql, null);
    }

    public Page<SubjectInfo> queryMemberBaseInfos(PageRequest pageRequest) {
        String hql = "from SubjectInfo order by createTime desc,sort asc";
        return subjectInfoDao.findPage(pageRequest, hql, null);
    }

    public Page<SubjectInfo> querySubjectInfos(PageRequest pageRequest, String hql) {
        return subjectInfoDao.findPage(pageRequest, hql, null);
    }

    public Map<String, Object> convertToMap(SubjectInfo subjectInfo) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (subjectInfo == null) {
            return map;
        }
        map.put("id", subjectInfo.getId());
        map.put("subject", TypeUtils.translateData("subject", subjectInfo.getSubject()));
        map.put("registerStatus", TypeUtils.translateData("registerStatus",subjectInfo.getRegisterStatus()));
        map.put("appreciationTaxType", subjectInfo.getAppreciationTaxType() == 1 ? "增值税普通发票" : "表示增值税专用发票");
        map.put("taxpayerNum", subjectInfo.getTaxpayerNum());
        map.put("addressName", subjectInfo.getAddressName());
        map.put("telephone", subjectInfo.getTelephone());
        map.put("bankAddress", subjectInfo.getBankAddress());
        map.put("bankAccount", subjectInfo.getBankAccount());
        map.put("taxIssue", subjectInfo.getTaxIssue());
        map.put("payTime", sdf.format(subjectInfo.getPayTime()));
        map.put("submitTime", sdf.format(subjectInfo.getSubmitTime()));
        map.put("modifyTime", sdf.format(subjectInfo.getModifyTime()));
        return map;
    }

    public PageBo<SubjectInfo> convert(Page<SubjectInfo> page) {
        return new PageBo<SubjectInfo>(page, new Convert<SubjectInfo>() {
            @Override
            public Map<String, Object> convert(SubjectInfo obj) {
                return convertToMap(obj);
            }
        });
    }

}
