package com.ruoyi.system.service.impl.project;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.project.BmProject;
import com.ruoyi.system.domain.project.BmProjectConstruction;
import com.ruoyi.system.mapper.project.BmProjectConstructionMapper;
import com.ruoyi.system.mapper.project.BmProjectMapper;
import com.ruoyi.system.service.project.IBmProjectConstructionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;


/**
 * 项目施工Service业务层处理
 *
 * @author ruoyi
 * @date 2021-03-08
 */
@Service
@Component
public class BmProjectConstructionServiceImpl implements IBmProjectConstructionService {

    @Autowired
    private BmProjectConstructionMapper bmProjectConstructionMapper;

    @Autowired
    private BmProjectMapper bmProjectMapper;

    private static BmProjectConstructionServiceImpl bmProjectConstructionServiceImpl;
    ;

    @PostConstruct
    public void init() {
        bmProjectConstructionServiceImpl = this;
        bmProjectConstructionServiceImpl.bmProjectMapper = this.bmProjectMapper;
    }

    /**
     * 查询项目施工
     *
     * @param id 项目施工ID
     * @return 项目施工
     */
    @Override
    public BmProjectConstruction selectBmProjectConstructionById(Long id) {
        return bmProjectConstructionMapper.selectBmProjectConstructionById(id);
    }

    /**
     * 查询项目施工列表
     *
     * @param bmProjectConstruction 项目施工
     * @return 项目施工
     */
    @Override
    public List<BmProjectConstruction> selectBmProjectConstructionList(BmProjectConstruction bmProjectConstruction) {
        return bmProjectConstructionMapper.selectBmProjectConstructionList(bmProjectConstruction);
    }

    /**
     * 新增项目施工
     *
     * @param bmProjectConstruction 项目施工
     * @return 结果
     */
    @Override
    public AjaxResult insertBmProjectConstruction(BmProjectConstruction bmProjectConstruction) {
        bmProjectConstruction.setCreateTime(DateUtils.getNowDate());
        //bmProjectConstruction.setCreateBy(SecurityUtils.getUsername());
        //提取出合同约定开工时间/竣工时间
        Long bmProjectId = bmProjectConstruction.getBmProjectId();
        if (bmProjectId == null) {
            return AjaxResult.error("缺少项目id");
        }
        templateSetTime(BmProjectConstruction.class, bmProjectConstruction);
        int i = bmProjectConstructionMapper.insertBmProjectConstruction(bmProjectConstruction);
        if (i != 1) {
            return AjaxResult.error("添加失败");
        }
        return AjaxResult.success();
    }

    /**
     * 修改项目施工
     *
     * @param bmProjectConstruction 项目施工
     * @return 结果
     */
    @Override
    public AjaxResult updateBmProjectConstruction(BmProjectConstruction bmProjectConstruction) {
        bmProjectConstruction.setUpdateTime(DateUtils.getNowDate());
        bmProjectConstruction.setUpdateBy(SecurityUtils.getUsername());
        //提取出合同约定开工时间/竣工时间
        Long bmProjectId = bmProjectConstruction.getBmProjectId();
        if (bmProjectId == null) {
            return AjaxResult.error("缺少项目id");
        }
        templateSetTime(BmProjectConstruction.class, bmProjectConstruction);
        int i = bmProjectConstructionMapper.updateBmProjectConstruction(bmProjectConstruction);
        if (i != 1) {
            return AjaxResult.error("更新失败");
        }
        return AjaxResult.success();
    }

    /**
     * 批量删除项目施工
     *
     * @param ids 需要删除的项目施工ID
     * @return 结果
     */
    @Override
    public int deleteBmProjectConstructionByIds(Long[] ids) {
        return bmProjectConstructionMapper.deleteBmProjectConstructionByIds(ids);
    }

    /**
     * 删除项目施工信息
     *
     * @param id 项目施工ID
     * @return 结果
     */
    @Override
    public int deleteBmProjectConstructionById(Long id) {
        return bmProjectConstructionMapper.deleteBmProjectConstructionById(id);
    }


    public static <T> void templateSetTime(Class<T> clazz, T cla) {
        T u = clazz.cast(cla);
        try {
            Class<?> aClass = u.getClass();
            Field bmProjectField = aClass.getDeclaredField("bmProjectId");
            Field orderBeginTimeField = aClass.getDeclaredField("orderBeginTime");
            Field orderEndTimeField = aClass.getDeclaredField("orderEndTime");
            bmProjectField.setAccessible(true);
            orderBeginTimeField.setAccessible(true);
            orderEndTimeField.setAccessible(true);
            String bmProjectId = bmProjectField.get(u).toString();
            String orderBeginTimeString = orderBeginTimeField.get(u).toString();
            String orderEndTimeString = orderEndTimeField.get(u).toString();
            BmProject bmProject = new BmProject();
            bmProject.setId(Long.valueOf(bmProjectId));
            if (orderBeginTimeString != null && orderEndTimeString != null) {
                bmProject.setOrderBeginTime(DateUtils.parse(orderBeginTimeString));
                bmProject.setOrderEndTime(DateUtils.parse(orderEndTimeString));
                bmProjectConstructionServiceImpl.bmProjectMapper.updateBmProject(bmProject);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
