package thyyxxk.webserver.dao.his.targetmanagement;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.archive.ArchiveUpload;
import thyyxxk.webserver.entity.targetmanagement.TargetDictTree;
import thyyxxk.webserver.entity.targetmanagement.TargetManagement;
import thyyxxk.webserver.entity.targetmanagement.ZbZdBaseInfo;

import java.util.List;

/**
 * @description: 指标字典
 * @author: hsh
 * @date: 2023/5/30 17:24
 **/
@Mapper
public interface ZbZdBaseInfoDao {

    @Select("<script> " +
            "select zb.* from zb_zd_base_info zb " +
            "where zb.state = 'Y' " +
            "<if test='id != null'> " +
            " and zb.id = #{id} " +
            "</if>" +
            "<if test='pid != null'> " +
            " and zb.pid = #{pid} " +
            "</if>" +
            "</script>")
    List<ZbZdBaseInfo> selectZbZdBaseInfo(TargetManagement dto);

    @Select("<script> " +
            "select zb.* from zb_zd_base_info zb " +
            "where zb.state = 'Y' " +
            " and zb.id = #{id} " +
            " and zb.pid = #{pid} " +
            "</script>")
    ZbZdBaseInfo selectZbZdBaseInfoById(TargetManagement target);

    @Select("<script> " +
            " with zb_tree_view as " +
            "( " +
            "select z1.id,z1.name,z1.pid,z1.sort,z1.dept_code,z1.is_leaf " +
            "from zb_zd_base_info z1 " +
            "where z1.id = 'B' and z1.state = 'Y' " +
            "union all " +
            "select z2.id,z2.name,z2.pid,z2.sort,z2.dept_code,z2.is_leaf " +
            "from zb_zd_base_info z2, zb_tree_view " +
            "where z2.pid = zb_tree_view.id and z2.state = 'Y' " +
            ") " +
            "select tree.id, tree.name as label, tree.pid, tree.sort, tree.dept_code as deptCode,tree.is_leaf " +
            "from zb_tree_view tree " +
            "order by len(case when charindex('-', tree.sort) = 0 then tree.sort else substring(tree.sort,1,charindex('-',tree.sort)-1) end) " +
            "</script>")
    List<TargetDictTree> selectTargetDictTree(TargetManagement dto);

    @Insert("insert into zb_zd_base_info (id, name, pid, sort, score, ratio, type, state, open_time, dept, dept_code, " +
            "op, op_id, definition, method, score_rule, source, is_leaf, child_id, mom_id) " +
            "values (#{id},#{name},#{pid},#{sort},#{score},#{ratio},#{type},#{state},#{openTime},#{dept},#{deptCode}, " +
            "#{op},#{opId},#{definition},#{method},#{scoreRule},#{source},#{isLeaf},#{childId},#{momId})")
    int saveZbZdBaseInfo(ZbZdBaseInfo zbZdBaseInfo);

    @Update("<script>" +
            "update zb_zd_base_info " +
            "<trim prefix=\"set\" suffixOverrides=\",\">" +
            "<if test=\"id != null\">" +
            "id = #{id}, " +
            "</if>" +
            "<if test=\"name != null \">" +
            "name = #{name}, " +
            "</if>" +
            "<if test=\"pid != null\">" +
            "pid = #{pid}, " +
            "</if>" +
            "<if test=\"sort != null\">" +
            "sort = #{sort}, " +
            "</if>" +
            "<if test=\"score != null\">" +
            "score = #{score}, " +
            "</if>" +
            "<if test=\"ratio != null\">" +
            "ratio = #{ratio}, " +
            "</if>" +
            "<if test=\"type != null\">" +
            "type = #{type}, " +
            "</if>" +
            "<if test=\"state != null\">" +
            "state = #{state}, " +
            "</if>" +
            "<if test=\"openTime != null\">" +
            "open_time = #{openTime}, " +
            "</if>" +
            "<if test=\"dept != null\">" +
            "dept = #{dept}, " +
            "</if>" +
            "<if test=\"deptCode != null\">" +
            "dept_code = #{deptCode}, " +
            "</if>" +
            "<if test=\"op != null\">" +
            "op = #{op}, " +
            "</if>" +
            "<if test=\"opId != null\">" +
            "op_id = #{opId}, " +
            "</if>" +
            "<if test=\"definition != null\">" +
            "definition = #{definition}, " +
            "</if>" +
            "<if test=\"method != null\">" +
            "method = #{method}, " +
            "</if>" +
            "<if test=\"scoreRule != null\">" +
            "score_rule = #{scoreRule}, " +
            "</if>" +
            "<if test=\"source != null\">" +
            "source = #{source}, " +
            "</if>" +
            "<if test=\"isLeaf != null\">" +
            "is_leaf = #{isLeaf}, " +
            "</if>" +
            "<if test=\"childId != null\">" +
            "child_id = #{childId}, " +
            "</if>" +
            "<if test=\"momId != null\">" +
            "mom_id = #{momId}, " +
            "</if>" +
            "</trim>" +
            "where id = #{id} and pid = #{pid} and state = 'Y' " +
            "</script>")
    int updateZbZdBaseInfo(ZbZdBaseInfo zbZdBaseInfo);

    @Update("<script> " +
            " delete from zb_zd_base_info where id = #{id} and pid = #{pid} " +
            "</script>")
    int deleteTargetDictById(@Param("id") String id, @Param("pid") String pid);

    @Update("<script> " +
            " update zb_zd_base_info " +
            "<trim prefix=\"set\" suffixOverrides=\",\">" +
            "<if test=\"childId != null\">" +
            "child_id = #{childId}, " +
            "</if>" +
            "<if test=\"momId != null \">" +
            "mom_id = #{momId}, " +
            "</if>" +
            "</trim>" +
            " where id = #{id} and pid = #{pid} and state = 'Y' " +
            "</script>")
    void updateZbZdBaseInfoCalcId(@Param("id") String id, @Param("pid") String pid, @Param("childId") String calcId, @Param("momId") String momId);

    @Select(" select * from zb_zd_base_info where pid = #{pid} order by id ")
    List<ZbZdBaseInfo> selectTargetDictScoreBySecond(@Param("pid") String pid);

    @Select(" select * from archive_upload where id = #{id} ")
    ArchiveUpload selectTargetFileById(@Param("id") String id);

}
