# coding:utf-8
import os
class PositionOutputer:
    def __init__(self):
        self.datas = list()
    def collect_data(self, position):
        if position is None:
            return
        self.datas.append(position)
    def clear_data(self):
        del self.datas[:]
    def output_html(self):
        fout = open('positionDetails.html', 'w')
        fout.write("<html>")
        fout.write("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />")
        fout.write("<body>")
        fout.write("<table>")
        fout.write("<tr>")
        fout.write("<td>职位名称</td>")
        fout.write("<td>工作地区</td>")
        fout.write("<td>学历</td>")
        fout.write("<td>职位描述</td>")
        fout.write("<td>职位人数</td>")
        fout.write("<td>有效起始日期</td>")
        fout.write("<td>有效截止日期</td>")
        fout.write("<td>实习或全职</td>")
        fout.write("<td>薪水</td>")
        fout.write("</tr>")
        for position in self.datas:
            fout.write("<tr>")
            fout.write("<td>%s</td>"%position.name.encode('utf-8'))
            fout.write("<td>%s</td>"%position.city.encode('utf-8'))
            fout.write("<td>%s</td>"%position.degree.encode('utf-8'))
            fout.write("<td>%s</td>"%position.desc.encode('utf-8'))
            fout.write("<td>%s</td>"%position.personNum.encode('utf-8'))
            fout.write("<td>%s</td>"%position.validStartDate.encode('utf-8'))
            fout.write("<td>%s</td>"%position.validEndDate.encode('utf-8'))
            fout.write("<td>%s</td>"%position.requirement.encode('utf-8'))
            fout.write("<td>%s</td><br/>"%position.salary.encode('utf-8'))
            fout.write("\n")
            fout.write("</tr>")
        fout.write("</table>")
        fout.write("</body>")
        fout.write("</html>")
        fout.close()
    def output_txt(self):
        file_name = 'positionDetails.txt'
        # 如果文件存在了，不输出表头
        if (os.path.exists(file_name)):
            fout = open(file_name, 'a')
        else:
            fout = open(file_name, 'w')
            fout.write("公司UUID\t")
            fout.write("职位URL\t")
            fout.write("职位名称\t")
            fout.write("实习或全职\t")
            fout.write("所属部门\t")
            fout.write("工作地区\t")
            fout.write("详细地点\t")
            fout.write("职位人数\t")
            fout.write("发布日期\t")
            fout.write("有效起始日期\t")
            fout.write("有效截止日期\t")
            fout.write("薪水\t")
            fout.write("学历\t")
            fout.write("英语要求\t")
            fout.write("性别\t")
            fout.write("年龄大于\t")
            fout.write("年龄小于\t")
            fout.write("职位描述\t")
            fout.write("\n")
        for position in self.datas:
            fout.write("%s\t" % position.company_uuid.encode('utf-8'))
            fout.write("%s\t" % position.url.encode('utf-8'))
            fout.write("%s\t"%position.name.encode('utf-8'))
            fout.write("%s\t" % position.requirement.encode('utf-8'))
            fout.write("%s\t" % position.department.encode('utf-8'))
            fout.write("%s\t" % position.city.encode('utf-8'))
            fout.write("%s\t" % position.detailPosition.encode('utf-8'))
            fout.write("%s\t"%position.personNum.encode('utf-8'))
            fout.write("%s\t"%position.publish_time.encode('utf-8'))
            fout.write("%s\t"%position.validStartDate.encode('utf-8'))
            fout.write("%s\t"%position.validEndDate.encode('utf-8'))
            fout.write("%s\t"%position.salary.encode('utf-8'))
            fout.write("%s\t" % position.degree.encode('utf-8'))
            fout.write("%s\t" % position.english_require.encode('utf-8'))
            fout.write("%s\t" % position.sex.encode('utf-8'))
            fout.write("%s\t" % position.startAge.encode('utf-8'))
            fout.write("%s\t" % position.endAge.encode('utf-8'))
            fout.write("%s\t" % position.desc.encode('utf-8'))
            fout.write("\n")
        fout.close()