<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.com.ty.lift.business.contract.dao.mapper.PaymentMapper" >
	<resultMap id="BaseResultMap" type="cn.com.ty.lift.business.contract.dao.entity.Payment" >
		<id column="id" property="id" jdbcType="BIGINT" />
		<result column="contracts_id" property="contractsId" jdbcType="BIGINT" />
		<result column="type" property="type" jdbcType="INTEGER" />
		<result column="summary" property="summary" jdbcType="VARCHAR" />
		<result column="plan_money" property="planMoney" jdbcType="DECIMAL" />
		<result column="work_money" property="workMoney" jdbcType="DECIMAL" />
		<result column="plan_date" property="planDate" jdbcType="TIMESTAMP" />
		<result column="work_date" property="workDate" jdbcType="TIMESTAMP" />
		<result column="cashier_user" property="cashierUser" jdbcType="BIGINT" />
		<result column="is_ticket" property="isTicket" jdbcType="INTEGER" />
		<result column="is_collection" property="isCollection" jdbcType="INTEGER" />
		<result column="remarks" property="remarks" jdbcType="LONGVARCHAR" />
		<result column="status" property="status" jdbcType="INTEGER" />
		<result column="note_img_url" property="noteImgUrl" jdbcType="LONGVARCHAR" />
		<result column="bank_img_url" property="bankImgUrl" jdbcType="LONGVARCHAR" />
		<result column="create_date" property="createDate" jdbcType="TIMESTAMP" />
		<result column="code" property="code" jdbcType="VARCHAR" />
		<result column="amount_invoice" property="amountInvoice" jdbcType="DECIMAL" />
		<result column="billing_date" property="billingDate" jdbcType="TIMESTAMP" />
		<result column="periods" property="periods" jdbcType="VARCHAR" />
		<result column="cost_name" property="CostName" jdbcType="VARCHAR" />
		<result column="cost_desc" property="CostDesc" jdbcType="VARCHAR" />
	</resultMap>

	<sql id="Base_Column_List" >
		id, contracts_id, type, summary, plan_money, work_money, plan_date, work_date, cashier_user, 
		is_ticket, is_collection, remarks, status, note_img_url, bank_img_url, create_date, code, amount_invoice, billing_date,
		periods, cost_name, cost_desc
	</sql>

	<select id="findProjectByCondition" parameterType="cn.com.ty.lift.business.contract.dao.entity.model.request.PaymentRequest"
			resultType="cn.com.ty.lift.business.contract.dao.entity.model.response.PaymentListResponse">
		SELECT
		       p.id                            AS projectId,
		       c.id                            AS contractId,
		       c.code                          AS contractCode,
			   p.project_name                  AS projectName,
			   c.plan_money_total              AS planMoney,
			   c.work_money_total              AS workMoney,
		       (c.plan_money_total - c.work_money_total) as uncollectedMoney,
		       c.end_date                      AS endDate,
		       ui.name                         AS cashierUser
		FROM contracts c
				 LEFT JOIN project p ON c.project_id = p.id
		         LEFT JOIN user_info ui on c.update_id = ui.user_id
		WHERE c.mt_company_id = #{request.mtCompanyId,jdbcType=BIGINT}
		AND c.type in('1','2','3','4')
		<if test="request.projectName!=null and request.projectName!=''">
			AND p.project_name LIKE CONCAT('%',#{request.projectName,jdbcType=VARCHAR},'%')
		</if>
	</select>

	<select id="findCapitalByCondition" parameterType="cn.com.ty.lift.business.contract.dao.entity.model.request.PaymentRequest"
			resultType="cn.com.ty.lift.business.contract.dao.entity.model.response.PaymentListResponse">
		SELECT
			cr.id                           AS projectId,
			c.id                            AS contractId,
			c.code                          AS contractCode,
			cr.project_name                 AS projectName,
			c.plan_money_total              AS planMoney,
			c.work_money_total              AS workMoney,
		   (c.plan_money_total - c.work_money_total) as uncollectedMoney,
			c.end_date                      AS endDate,
			ui.name                         AS cashierUser
		FROM contracts c
			LEFT JOIN capital_repair cr ON c.project_id = cr.id
		    LEFT JOIN user_info ui on c.update_id = ui.user_id
		WHERE c.mt_company_id = #{request.mtCompanyId,jdbcType=BIGINT}
		AND c.type = '5'
		<if test="request.projectName!=null and request.projectName!=''">
			AND cr.project_name LIKE CONCAT('%',#{request.projectName,jdbcType=VARCHAR},'%')
		</if>
	</select>

	<select id="queryProjectDetailById" parameterType="cn.com.ty.lift.business.contract.dao.entity.model.request.PaymentRequest"
			resultType="cn.com.ty.lift.business.contract.dao.entity.model.response.PaymentDetailResponse">
		SELECT
		       p.project_name AS projectName,
			   p.address      AS projectAddress,
			   c.code         AS contractCode,
			   c.source       AS contractSource,
			   c.type         AS contractType,
			   c.lift_num     AS liftNum,
			   c.star_date    AS starDate,
			   c.end_date     AS endDate,
			   c.pay_mode     AS payMode,
			   c.moneys       AS moneys,
			   c.given_date   AS givenDate,
			   c.given_user   AS givenUser,
			   c.remarks      AS remarks
		FROM contracts c
				 LEFT JOIN project p ON c.project_id = p.id
		WHERE c.mt_company_id = #{request.mtCompanyId,jdbcType=BIGINT}
		<if test="request.id!=null and request.id!=''">
			AND c.id =  #{request.id,jdbcType=BIGINT}
		</if>
	</select>

	<select id="queryCapitalDetailById" parameterType="cn.com.ty.lift.business.contract.dao.entity.model.request.PaymentRequest"
			resultType="cn.com.ty.lift.business.contract.dao.entity.model.response.PaymentDetailResponse">
		SELECT
		cr.project_name AS projectName,
		cr.project_address AS projectAddress,
		c.code         AS contractCode,
		c.source       AS contractSource,
		c.type         AS contractType,
		c.lift_num     AS liftNum,
		c.star_date    AS starDate,
		c.end_date     AS endDate,
		c.pay_mode     AS payMode,
		c.moneys       AS moneys,
		c.given_date   AS givenDate,
		c.given_user   AS givenUser,
		c.remarks      AS remarks
		FROM contracts c
		LEFT JOIN capital_repair cr ON c.project_id = cr.id
		WHERE c.mt_company_id = #{request.mtCompanyId,jdbcType=BIGINT}
		<if test="request.id!=null and request.id!=''">
			AND c.id =  #{request.id,jdbcType=BIGINT}
		</if>
	</select>

</mapper>