from lession_10.opration_log import configlog
from lession_9.homework.operation_excel import OperationExcel
import time
import time
import random
from dateutil import rrule
import datetime
from multiprocessing import Process
from lession_9.user_log import User_log

import logging


class ClaculateWages:
    def __init__(self, file_name, sheet_name, staff_num: int = 0, staff_cut_off_time: str = '',
                 staff_name: str = '', staff_info: str = '', tation: str = '', tation_and_salary: list = '',
                 exception: str = '', total_sala: str = '', start_time: str = '', end_time: str = ''):
        self.staff_num = staff_num  # 员工编号
        self.staff_cut_off_time = staff_cut_off_time  # 工资算时间
        self.staff_name = staff_name  # 员工姓名
        self.staff_info = staff_info  # 员工基本信息结
        self.tation_salar = tation_and_salary
        self.tation = tation  # 岗位
        self.exception = exception  # 说明
        self.total_sala = total_sala  # 工资总计
        self.start_time = start_time
        self.end_time = end_time
        self.poer = OperationExcel(file_name, sheet_name)
        u = configlog()
        self.logger = u.get_log()

    # 讲计算结果写入excel
    def write_info(self):
        time.sleep(random.randint(1,1))
        self.salary_num()
        try:
            self.data = {
                "员工编号": self.staff_num,
                "工资结算时间": self.staff_cut_off_time,
                "员工姓名": self.staff_name,
                "员工基本信息": self.staff_info,
                "岗位": self.tation,
                "工资": self.tat_sala,
                "说明": self.exception,
                "工资总计": self.salary_num(),
            }
        except AttributeError:
            pass
        lis = []
        try:
            for li in self.data.values():
                lis.append(li)
            num = self.poer.get_lines()
            for i in range(len(lis)):
                if self.salary_num() > 0:
                    self.poer.write_excel(num, i, lis[i])
                    # for y in range(len(self.poer.get_rols(2))-1):
                    #     if self.poer.get_rols(2)[y] == self.poer.get_rols(2)[y+1]:
                    #         return y
                    if self.poer.get_cell_data(num, 2) == self.poer.get_cell_data(num - 1, 2):
                        total = int(self.poer.get_cell_data(num - 1, 7)) + self.salary_num()
                        self.poer.write_excel(num, 7, total)
                        if i == len(lis) - 1:
                            lis[i] = ""
                            self.poer.write_excel(num - 1, 7, lis[i])

        except AttributeError as e:
            logging.info('岗位名称不合法' + '--->' +self.tation+'  '+ str(e))
            # 计算工资

    def salary_num(self):
        try:
            if self.tation == '程序员':
                self.tat_sala = self.tation_salar[0][self.tation]
            if self.tation == '扫地':
                self.tat_sala = self.tation_salar[1][self.tation]
            if (self.days_between() // 7) >= 1:
                sala = round(self.tat_sala * (self.days_between() / 7))
                return sala
            else:
                return 0
        except AttributeError:
            pass

        # 计算工作时间

    def days_between(self):
        days = rrule.rrule(rrule.DAILY, dtstart=self.start_time, until=self.end_time)
        return days.count()


if __name__ == '__main__':
    c = ClaculateWages('员工工资表.xls', 'Sheet1')
    c.staff_num = 3
    c.staff_cut_off_time = '2019-01-01'
    c.staff_name = '王五'
    c.staff_info = '居住在回龙观 '
    c.start_time = datetime.date(2019, 1, 21)
    c.end_time = datetime.date(2019, 1, 2)
    c.tation_salar = [{"程序员": 2000}, {"扫地": 1000}]
    c.tation ='程序员22'
    c.exception = '工资结算4周'
    # c.write_info()
    pro1 = Process(target=c.write_info)
    pro1.start()
    pro1.join()


    c.staff_num = 3
    c.staff_cut_off_time = '2019-01-01'
    c.staff_name = '王五'
    c.staff_info = '居住在回龙观 '
    c.start_time = datetime.date(2019, 1, 1)
    c.end_time = datetime.date(2019, 1, 12)
    c.tation_salar = [{"程序员": 2000}, {"扫地": 1000}]
    c.tation = '扫地'
    c.exception = '工资结算4周'
    # c.write_info()
    pro2 = Process(target=c.write_info)
    pro2.start()
    pro2.join()

    c.staff_num = 2
    c.staff_cut_off_time = '2019-01-01'
    c.staff_name = '张三'
    c.staff_info = '居住在回龙观 '
    c.start_time = datetime.date(2019, 1, 1)
    c.end_time = datetime.date(2019, 1, 31)
    c.tation_salar = [{"程序员": 2000}, {"扫地": 1000}]
    c.tation = '扫地'
    c.exception = '工资结算4周'
    pro3 = Process(target=c.write_info)
    pro3.start()
    pro3.join()

    c.staff_num = 2
    c.staff_cut_off_time = '2019-01-01'
    c.staff_name = '丽莎娜'
    c.staff_info = '居住在回龙观 '
    c.start_time = datetime.date(2019, 1, 1)
    c.end_time = datetime.date(2019, 1, 31)
    c.tation_salar = [{"程序员": 2000}, {"扫地": 1000}]
    c.tation = '程序员'
    c.exception = '工资结算4周'
    pro4 = Process(target=c.write_info)
    pro4.start()
    pro4.join()