package pwc.taxtech.atms.vat.dao;

import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.session.RowBounds;
import pwc.taxtech.atms.MyVatMapper;
import pwc.taxtech.atms.vat.dpo.ApprovalTaskInfo;
import pwc.taxtech.atms.vat.entity.PeriodApprove;
import pwc.taxtech.atms.vat.entity.PeriodApproveExample;

@Mapper
public interface PeriodApproveMapper extends MyVatMapper {
    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table TAX_ADMIN.PERIOD_APPROVE
     *
     * @mbg.generated
     */
    long countByExample(PeriodApproveExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table TAX_ADMIN.PERIOD_APPROVE
     *
     * @mbg.generated
     */
    int deleteByExample(PeriodApproveExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table TAX_ADMIN.PERIOD_APPROVE
     *
     * @mbg.generated
     */
    int deleteByPrimaryKey(String id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table TAX_ADMIN.PERIOD_APPROVE
     *
     * @mbg.generated
     */
    int insert(PeriodApprove record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table TAX_ADMIN.PERIOD_APPROVE
     *
     * @mbg.generated
     */
    int insertSelective(PeriodApprove record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table TAX_ADMIN.PERIOD_APPROVE
     *
     * @mbg.generated
     */
    List<PeriodApprove> selectByExampleWithRowbounds(PeriodApproveExample example, RowBounds rowBounds);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table TAX_ADMIN.PERIOD_APPROVE
     *
     * @mbg.generated
     */
    List<PeriodApprove> selectByExample(PeriodApproveExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table TAX_ADMIN.PERIOD_APPROVE
     *
     * @mbg.generated
     */
    PeriodApprove selectByPrimaryKey(String id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table TAX_ADMIN.PERIOD_APPROVE
     *
     * @mbg.generated
     */
    int updateByExampleSelective(@Param("record") PeriodApprove record, @Param("example") PeriodApproveExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table TAX_ADMIN.PERIOD_APPROVE
     *
     * @mbg.generated
     */
    int updateByExample(@Param("record") PeriodApprove record, @Param("example") PeriodApproveExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table TAX_ADMIN.PERIOD_APPROVE
     *
     * @mbg.generated
     */
    int updateByPrimaryKeySelective(PeriodApprove record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table TAX_ADMIN.PERIOD_APPROVE
     *
     * @mbg.generated
     */
    int updateByPrimaryKey(PeriodApprove record);
    
    @Select("<script>" +
            "SELECT " +
            " p.organization_id AS organizationId, " +
            " p.name AS projectName, " +
            " pa.project_id AS projectId, " +
            " pa.period AS period, " +
            " pa.instance_id AS instanceId, " +
            " u1.user_name AS createBy, " +
            " u2.user_name AS approvalBy, " +
            " pa.status AS status, " +
            " pa.approval_resualt AS approvalResult, " +
            " pa.create_time AS createTime, " +
            " pa.approval_time AS approvalTime, " +
            " pa.report_paths AS reportPaths," +
            " pa.report_ids as reportIds, " +
            " pa.template_ids as templateIds " +
            "FROM " +
            " period_approve pa " +
            " INNER JOIN project p ON pa.project_id = p.ID " +
            " JOIN user u1 ON pa.create_by = u1.id " +
            " LEFT JOIN user u2 ON pa.approval_by = u2.id  " +
            "</script>")
   List<ApprovalTaskInfo> queryApprovalList();

    @Select("" +
            "SELECT  " +
            "  p.status AS status   " +
            "FROM  " +
            "  ( SELECT * FROM period_approve WHERE project_id = #{projectId} AND period = #{period} ORDER BY create_time DESC ) p  " +
            "LIMIT 1"+
            "")
    String getStatusByProjectIdAndPeriod(@Param("projectId") String projectId, @Param("period") Integer period);

    @Select("" +
            "SELECT  " +
            "  p.instance_id AS instanceId ,p.status AS status " +
            "FROM  " +
            "  ( SELECT * FROM period_approve WHERE project_id = #{projectId} AND period = #{period} ORDER BY create_time DESC )  p " +
            "LIMIT 1"+
            "")
    PeriodApprove getInstanceIdByProjectIdAndPeriod(@Param("projectId") String projectId, @Param("period") Integer period);
}